From 2b990a3cd974997906280c23cb79fa38dae25ede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20Rosales?= Date: Tue, 3 Oct 2023 11:51:47 -0600 Subject: [PATCH] feat: add errors for connection timeout and lack of dburl --- src/event-db/src/error.rs | 9 ++++++++- src/event-db/src/lib.rs | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/event-db/src/error.rs b/src/event-db/src/error.rs index 8c5064ae19..1be7ee229d 100644 --- a/src/event-db/src/error.rs +++ b/src/event-db/src/error.rs @@ -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}")] @@ -17,7 +21,10 @@ pub enum Error { impl From> for Error { fn from(val: RunError) -> Self { - Self::Unknown(val.to_string()) + match val { + RunError::TimedOut => Self::ConnectionTimeout, + _ => Self::Unknown(val.to_string()), + } } } diff --git a/src/event-db/src/lib.rs b/src/event-db/src/lib.rs index ee7d27b687..eea57880e8 100644 --- a/src/event-db/src/lib.rs +++ b/src/event-db/src/lib.rs @@ -59,7 +59,7 @@ pub async fn establish_connection(url: Option<&str>) -> Result { 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)?;