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.
fast-insiders/server/src/error.rs

50 lines
1.3 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),
}
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!("Not found error: {}", e)),
};
let body = Json(json!({
"error": error_message,
}));
(status, body).into_response()
}
}