1use axum::http::StatusCode;
6use axum::response::{IntoResponse, Response};
7use serde_json::json;
8
9pub enum ApiError {
11 Storage(tuitbot_core::error::StorageError),
13 NotFound(String),
15 BadRequest(String),
17 Conflict(String),
19}
20
21impl From<tuitbot_core::error::StorageError> for ApiError {
22 fn from(err: tuitbot_core::error::StorageError) -> Self {
23 Self::Storage(err)
24 }
25}
26
27impl IntoResponse for ApiError {
28 fn into_response(self) -> Response {
29 let (status, message) = match self {
30 Self::Storage(e) => {
31 tracing::error!("storage error: {e}");
32 (StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
33 }
34 Self::NotFound(msg) => (StatusCode::NOT_FOUND, msg),
35 Self::BadRequest(msg) => (StatusCode::BAD_REQUEST, msg),
36 Self::Conflict(msg) => (StatusCode::CONFLICT, msg),
37 };
38
39 let body = axum::Json(json!({ "error": message }));
40 (status, body).into_response()
41 }
42}