Skip to content

Commit

Permalink
feat: add errors for connection timeout and lack of dburl
Browse files Browse the repository at this point in the history
  • Loading branch information
saibatizoku committed Oct 3, 2023
1 parent 0703867 commit 2b990a3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/event-db/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ use bb8::RunError;
/// Event database errors
#[derive(thiserror::Error, Debug, PartialEq, Eq)]
pub enum Error {
#[error("DB connection timed out")]
ConnectionTimeout,
#[error(" Schema in database does not match schema supported by the Crate. The current schema version: {was}, the schema version we expected: {expected}")]
MismatchedSchema { was: i32, expected: i32 },
#[error("DB URL is undefined")]
NoDatabaseUrl,
#[error("Cannot find this item, error: {0}")]
NotFound(String),
#[error("error: {0}")]
Expand All @@ -17,7 +21,10 @@ pub enum Error {

impl From<RunError<tokio_postgres::Error>> for Error {
fn from(val: RunError<tokio_postgres::Error>) -> Self {
Self::Unknown(val.to_string())
match val {
RunError::TimedOut => Self::ConnectionTimeout,
_ => Self::Unknown(val.to_string()),
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/event-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub async fn establish_connection(url: Option<&str>) -> Result<EventDB, Error> {
let database_url = match url {
Some(url) => url.to_string(),
// If the Database connection URL is not supplied, try and get from the env var.
None => std::env::var(DATABASE_URL_ENVVAR)?,
None => std::env::var(DATABASE_URL_ENVVAR).map_err(|_| Error::NoDatabaseUrl)?,
};

let config = tokio_postgres::config::Config::from_str(&database_url)?;
Expand Down

0 comments on commit 2b990a3

Please sign in to comment.