diff --git a/apps/api/src/errors/service_error.rs b/apps/api/src/errors/service_error.rs index bd99ad8..f04ea40 100644 --- a/apps/api/src/errors/service_error.rs +++ b/apps/api/src/errors/service_error.rs @@ -1,3 +1,4 @@ +use axum::response::IntoResponse; use sea_orm::DbErr; #[derive(Debug)] @@ -37,3 +38,23 @@ impl From for ServiceError { } } } + +impl IntoResponse for ServiceError { + fn into_response(self) -> axum::response::Response { + let (status, message) = match &self { + ServiceError::NotFound(msg) => (axum::http::StatusCode::NOT_FOUND, msg.clone()), + ServiceError::DatabaseError(msg) => { + (axum::http::StatusCode::INTERNAL_SERVER_ERROR, msg.clone()) + } + ServiceError::Unauthorized(msg) => (axum::http::StatusCode::UNAUTHORIZED, msg.clone()), + ServiceError::InternalError(msg) => { + (axum::http::StatusCode::INTERNAL_SERVER_ERROR, msg.clone()) + } + ServiceError::BadRequest(msg) => (axum::http::StatusCode::BAD_REQUEST, msg.clone()), + }; + let body = axum::Json(serde_json::json!({ + "error": message, + })); + (status, body).into_response() + } +}