40 lines
1003 B
Rust
40 lines
1003 B
Rust
use axum::{
|
|
Json,
|
|
http::StatusCode,
|
|
response::{IntoResponse, Response},
|
|
};
|
|
use serde_json::json;
|
|
use thiserror::Error;
|
|
|
|
#[derive(Error, Debug)]
|
|
pub enum AppError {
|
|
#[error("Not Found: {0}")]
|
|
NotFound(String),
|
|
|
|
#[error("Forbidden: {0}")]
|
|
Forbidden(String),
|
|
|
|
#[error("Internal Server Error: {0}")]
|
|
Internal(String),
|
|
|
|
#[error("Bad Request: {0}")]
|
|
BadRequest(String),
|
|
}
|
|
|
|
impl IntoResponse for AppError {
|
|
fn into_response(self) -> Response {
|
|
let (status, error_message) = match &self {
|
|
AppError::NotFound(msg) => (StatusCode::NOT_FOUND, msg.clone()),
|
|
AppError::Forbidden(msg) => (StatusCode::FORBIDDEN, msg.clone()),
|
|
AppError::Internal(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg.clone()),
|
|
AppError::BadRequest(msg) => (StatusCode::BAD_REQUEST, msg.clone()),
|
|
};
|
|
|
|
let body = Json(json!({
|
|
"error": error_message,
|
|
}));
|
|
|
|
(status, body).into_response()
|
|
}
|
|
}
|