Files
lan-play/src/error.rs
T
Ultradesu eb0d4fb1c5
Build and Publish / Build and Publish Docker Image (push) Successful in 10m8s
Init
2026-08-05 12:32:52 +01:00

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()
}
}