diff options
Diffstat (limited to 'src/errors.rs')
-rw-r--r-- | src/errors.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/errors.rs b/src/errors.rs index 1b9a802..3dda65c 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,3 +1,5 @@ +use core::fmt; + use axum::{ http::StatusCode, response::{IntoResponse, Response}, @@ -66,3 +68,31 @@ impl From<std::io::Error> for AppError { AppError::BadRequest(error.to_string()) } } + +/// Implementation of the `{}` marker for AppError +impl fmt::Display for AppError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + AppError::Database => write!(f, "Database"), + AppError::BadRequest(value) => write!(f, "BadRequest: {}", value), + AppError::NotFound(value) => write!(f, "Not found: {}", value), + AppError::TokenCreation => write!(f, "Token creation"), + AppError::InvalidToken => write!(f, "Invalid Token"), + AppError::Unauthorized => write!(f, "Unauthorized"), + } + } +} + +/// 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 + } +} + +/// A async_graphql error is mapped to an `AppError::BadRequest` +impl From<async_graphql::Error> for AppError { + fn from(value: async_graphql::Error) -> Self { + AppError::BadRequest(value.message) + } +} |