Spaces:
Sleeping
Sleeping
| use axum::{ | |
| http::StatusCode, | |
| response::{IntoResponse, Response}, | |
| Json, | |
| }; | |
| use serde_json::json; | |
| pub enum AppError { | |
| Internal(String), | |
| BadRequest(String), | |
| Unauthorized(String), | |
| Forbidden(String), | |
| NotFound(String), | |
| DbError(sqlx::Error), | |
| } | |
| impl From<sqlx::Error> for AppError { | |
| fn from(err: sqlx::Error) -> Self { | |
| AppError::DbError(err) | |
| } | |
| } | |
| impl IntoResponse for AppError { | |
| fn into_response(self) -> Response { | |
| let (status, message) = match self { | |
| AppError::Internal(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg), | |
| AppError::BadRequest(msg) => (StatusCode::BAD_REQUEST, msg), | |
| AppError::Unauthorized(msg) => (StatusCode::UNAUTHORIZED, msg), | |
| AppError::Forbidden(msg) => (StatusCode::FORBIDDEN, msg), | |
| AppError::NotFound(msg) => (StatusCode::NOT_FOUND, msg), | |
| AppError::DbError(err) => ( | |
| StatusCode::INTERNAL_SERVER_ERROR, | |
| format!("Database error: {}", err), | |
| ), | |
| }; | |
| let body = Json(json!({ | |
| "error": message | |
| })); | |
| (status, body).into_response() | |
| } | |
| } | |