diff options
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/src/error.rs b/src/error.rs index 1f4b354..a5b48ff 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,26 +1,46 @@ -use axum::{ - http::StatusCode, - response::{IntoResponse, Response}, - Json, -}; - pub type Result<T, E = Error> = std::result::Result<T, E>; #[derive(thiserror::Error, Debug)] pub enum Error { - #[error("IO error: {0}")] + #[error(transparent)] IO(#[from] std::io::Error), - #[error("Axum Error: {0:?}")] + #[error(transparent)] + TaskJoin(#[from] tokio::task::JoinError), + + #[error(transparent)] Axum(#[from] axum::Error), + + #[error(transparent)] + Sqlx(#[from] sqlx::Error), + + #[error(transparent)] + Migration(#[from] sqlx::migrate::MigrateError), + + #[error("User not found: {0}")] + UserNotFound(uuid::Uuid), } -impl IntoResponse for Error { - fn into_response(self) -> Response { - let body = Json(serde_json::json!({ - "error": self.to_string(), - })); +impl axum::response::IntoResponse for Error { + fn into_response(self) -> axum::response::Response { + use axum::{http::StatusCode, Json}; + use serde_json::json; - (StatusCode::INTERNAL_SERVER_ERROR, body).into_response() + match self { + Error::UserNotFound(uuid) => ( + StatusCode::BAD_REQUEST, + Json(json!({ + "status": "fail", + "message": uuid, + })), + ), + err => ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(json!({ + "error": err.to_string(), + })), + ), + } + .into_response() } } |