mabel/src/error.rs

44 lines
971 B
Rust

use std::error::Error;
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use serde_json::json;
pub enum AppError {
ServerError(anyhow::Error),
ClientError {
status: StatusCode,
code: String,
message: String,
},
}
impl IntoResponse for AppError {
fn into_response(self) -> Response {
match self {
AppError::ServerError(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"code":"server-error", "message": err.to_string()})),
)
.into_response(),
AppError::ClientError {
status,
code,
message,
} => (status, Json(json!({"code":code, "message": message}))).into_response(),
}
}
}
impl<E> From<E> for AppError
where
E: Into<anyhow::Error>,
{
fn from(err: E) -> Self {
AppError::ServerError(err.into())
}
}