#[derive(Debug, thiserror::Error)]
pub enum ServerError {
#[error("Database error: {0}")]
Database(String),
#[error("Not found: {0}")]
NotFound(String),
#[error("Bad request: {0}")]
BadRequest(String),
#[error("Conflict: {0}")]
Conflict(String),
#[error("Internal error: {0}")]
Internal(String),
#[error("Not implemented: {0}")]
NotImplemented(String),
}
#[cfg(feature = "axum")]
impl axum::response::IntoResponse for ServerError {
fn into_response(self) -> axum::response::Response {
use axum::http::StatusCode;
let (status, message) = match &self {
ServerError::Database(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg.clone()),
ServerError::NotFound(msg) => (StatusCode::NOT_FOUND, msg.clone()),
ServerError::BadRequest(msg) => (StatusCode::BAD_REQUEST, msg.clone()),
ServerError::Conflict(msg) => (StatusCode::CONFLICT, msg.clone()),
ServerError::Internal(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg.clone()),
ServerError::NotImplemented(msg) => (StatusCode::NOT_IMPLEMENTED, msg.clone()),
};
let body = serde_json::json!({ "error": message });
(status, axum::Json(body)).into_response()
}
}