From 7ac336871545279d575305b521e2ccadf20e9f86 Mon Sep 17 00:00:00 2001 From: GW_MC <72297530+GWMCwing@users.noreply.github.com> Date: Mon, 29 Dec 2025 15:18:40 +0800 Subject: [PATCH] feat: added intoResponse --- apps/api/src/errors/service_error.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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() + } +}