use axum::{ http::StatusCode, response::{IntoResponse, Response}, Json, }; use sea_orm::DbErr; use serde_json::json; use crate::repo::in_process_transaction::InProcessTransactionError; pub enum AppError { DbErr(DbErr), InProcessTransaction(InProcessTransactionError), NotFound(String), } impl From for AppError { fn from(inner: DbErr) -> Self { AppError::DbErr(inner) } } impl From for AppError { fn from(inner: InProcessTransactionError) -> Self { AppError::InProcessTransaction(inner) } } impl IntoResponse for AppError { fn into_response(self) -> Response { let (status, error_message) = match self { AppError::DbErr(e) => ( StatusCode::INTERNAL_SERVER_ERROR, format!("The database retruned an error: {}", e), ), AppError::InProcessTransaction(e) => ( StatusCode::INTERNAL_SERVER_ERROR, format!("Error in the in process transaction repo: {}", e), ), AppError::NotFound(e) => (StatusCode::NOT_FOUND, format!("Not found error: {}", e)), }; let body = Json(json!({ "error": error_message, })); (status, body).into_response() } }