You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.6 KiB
56 lines
1.6 KiB
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),
|
|
InternalServerError(String),
|
|
Unauthorized,
|
|
Conflict(String),
|
|
}
|
|
|
|
impl From<DbErr> for AppError {
|
|
fn from(inner: DbErr) -> Self {
|
|
AppError::DbErr(inner)
|
|
}
|
|
}
|
|
|
|
impl From<InProcessTransactionError> 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!("{e}")),
|
|
AppError::InternalServerError(e) => (StatusCode::INTERNAL_SERVER_ERROR, e),
|
|
AppError::Unauthorized => (StatusCode::UNAUTHORIZED, "Unauthorized".to_string()),
|
|
AppError::Conflict(e) => (StatusCode::CONFLICT, e),
|
|
};
|
|
|
|
let body = Json(json!({
|
|
"error": error_message,
|
|
}));
|
|
|
|
(status, body).into_response()
|
|
}
|
|
}
|