pub mod api;
pub mod handlers;
pub mod models;
pub use api::create_app;
pub use handlers::*;
pub use models::*;
pub type Result<T> = std::result::Result<T, ApiError>;
#[derive(Debug)]
pub enum ApiError {
Collection(String),
Internal(String),
BadRequest(String),
}
impl std::fmt::Display for ApiError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ApiError::Collection(msg) => write!(f, "Collection error: {}", msg),
ApiError::Internal(msg) => write!(f, "Internal error: {}", msg),
ApiError::BadRequest(msg) => write!(f, "Bad request: {}", msg),
}
}
}
impl std::error::Error for ApiError {}
impl axum::response::IntoResponse for ApiError {
fn into_response(self) -> axum::response::Response {
let (status, error_message) = match self {
ApiError::Collection(msg) => (axum::http::StatusCode::INTERNAL_SERVER_ERROR, msg),
ApiError::Internal(msg) => (axum::http::StatusCode::INTERNAL_SERVER_ERROR, msg),
ApiError::BadRequest(msg) => (axum::http::StatusCode::BAD_REQUEST, msg),
};
let body = serde_json::json!({
"error": error_message
});
(status, axum::Json(body)).into_response()
}
}