pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Database error: {0}")]
Database(#[from] rusqlite::Error),
#[error("Configuration error: {0}")]
Config(String),
#[error("Not found: {0}")]
NotFound(String),
#[error("Internal error: {0}")]
Internal(#[from] anyhow::Error),
}
#[cfg(feature = "server")]
impl axum::response::IntoResponse for Error {
fn into_response(self) -> axum::response::Response {
use axum::Json;
use axum::http::StatusCode;
use serde_json::json;
let (status, message) = match &self {
Error::NotFound(msg) => (StatusCode::NOT_FOUND, msg.clone()),
Error::Database(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()),
Error::Config(msg) => (StatusCode::BAD_REQUEST, msg.clone()),
Error::Internal(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()),
};
let body = Json(json!({
"error": {
"message": message,
"code": status.as_u16(),
}
}));
(status, body).into_response()
}
}