diff options
author | Santo Cariotti <santo@dcariotti.me> | 2024-12-21 20:06:33 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2024-12-21 20:06:33 +0100 |
commit | 1fdce941bb27451f879a36d14e2c3dcb7742a191 (patch) | |
tree | 211745c8cdf143af60e79f82e9871df4edeea278 | |
parent | a785f313f93f2d3b76a63c3adbd81f8fff1271b0 (diff) |
Show possible error on app creation
-rw-r--r-- | src/errors.rs | 12 | ||||
-rw-r--r-- | src/graphql/types/notification.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 31 |
3 files changed, 24 insertions, 21 deletions
diff --git a/src/errors.rs b/src/errors.rs index 5f2a8dd..5292d06 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -11,7 +11,7 @@ use serde_json::json; #[derive(Debug)] pub enum AppError { /// Database error - Database, + Database(String), /// Generic bad request. It is handled with a message value BadRequest(String), /// Not found error @@ -33,9 +33,9 @@ impl IntoResponse for AppError { /// ``` fn into_response(self) -> Response { let (status, error_message) = match self { - AppError::Database => ( + AppError::Database(value) => ( StatusCode::INTERNAL_SERVER_ERROR, - "Error with database connection".to_string(), + format!("Error with database connection: {value}"), ), AppError::BadRequest(value) => (StatusCode::BAD_REQUEST, value), AppError::NotFound(value) => (StatusCode::NOT_FOUND, value), @@ -73,7 +73,7 @@ impl From<std::io::Error> for AppError { impl fmt::Display for AppError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - AppError::Database => write!(f, "Database"), + AppError::Database(value) => write!(f, "Database: {}", value), AppError::BadRequest(value) => write!(f, "BadRequest: {}", value), AppError::NotFound(value) => write!(f, "Not found: {}", value), AppError::TokenCreation => write!(f, "Token creation"), @@ -85,8 +85,8 @@ impl fmt::Display for AppError { /// A tokio_postgres error is mapped to an `AppError::Database` impl From<tokio_postgres::Error> for AppError { - fn from(_: tokio_postgres::Error) -> Self { - AppError::Database + fn from(value: tokio_postgres::Error) -> Self { + AppError::Database(value.to_string()) } } diff --git a/src/graphql/types/notification.rs b/src/graphql/types/notification.rs index fc71c11..21fc1aa 100644 --- a/src/graphql/types/notification.rs +++ b/src/graphql/types/notification.rs @@ -128,7 +128,7 @@ impl Notification { let row = rows[0].clone(); Ok(row.get("id")) } - Err(_) => Err(AppError::Database), + Err(e) => Err(AppError::Database(e.to_string())), } } } diff --git a/src/main.rs b/src/main.rs index b9b7f87..bf8afda 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,21 +81,24 @@ async fn create_app() -> Result<Router, AppError> { #[tokio::main(flavor = "current_thread")] async fn main() { - if let Ok(app) = create_app().await { - let host = &CONFIG.allowed_host; + match create_app().await { + Ok(app) => { + let host = &CONFIG.allowed_host; - let addr = match host.parse::<SocketAddr>() { - Ok(addr) => addr, - Err(e) => { - panic!("`{}` {}", host, e); - } - }; - tracing::info!("Listening on {}", addr); + let addr = match host.parse::<SocketAddr>() { + Ok(addr) => addr, + Err(e) => { + panic!("`{}` {}", host, e); + } + }; + tracing::info!("Listening on {}", addr); - axum::serve(TcpListener::bind(&addr).await.unwrap(), app) - .await - .unwrap(); - } else { - tracing::error!("Can't create an application!"); + axum::serve(TcpListener::bind(&addr).await.unwrap(), app) + .await + .unwrap(); + } + Err(e) => { + tracing::error!("Can't create an application: {}", e); + } } } |