43 lines
973 B
Rust
43 lines
973 B
Rust
use axum::{
|
|
Json,
|
|
http::StatusCode,
|
|
response::{IntoResponse, Response},
|
|
};
|
|
use serde_json::json;
|
|
|
|
#[derive(Debug)]
|
|
pub struct AppError {
|
|
status: StatusCode,
|
|
message: String,
|
|
}
|
|
|
|
impl AppError {
|
|
pub fn bad_request(message: impl Into<String>) -> Self {
|
|
Self {
|
|
status: StatusCode::BAD_REQUEST,
|
|
message: message.into(),
|
|
}
|
|
}
|
|
|
|
pub fn not_found(message: impl Into<String>) -> Self {
|
|
Self {
|
|
status: StatusCode::NOT_FOUND,
|
|
message: message.into(),
|
|
}
|
|
}
|
|
|
|
pub fn internal(error: impl std::fmt::Display) -> Self {
|
|
tracing::error!(%error, "request failed");
|
|
Self {
|
|
status: StatusCode::INTERNAL_SERVER_ERROR,
|
|
message: "internal server error".into(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl IntoResponse for AppError {
|
|
fn into_response(self) -> Response {
|
|
(self.status, Json(json!({ "error": self.message }))).into_response()
|
|
}
|
|
}
|