Skip to main content

aster_server/routes/
errors.rs

1use aster::config::ConfigError;
2use axum::{
3    http::StatusCode,
4    response::{IntoResponse, Response},
5    Json,
6};
7use serde::Serialize;
8use utoipa::ToSchema;
9
10#[derive(Debug, Serialize, ToSchema)]
11pub struct ErrorResponse {
12    pub message: String,
13    #[serde(skip)]
14    pub status: StatusCode,
15}
16
17impl ErrorResponse {
18    pub(crate) fn internal(message: impl Into<String>) -> Self {
19        Self {
20            message: message.into(),
21            status: StatusCode::INTERNAL_SERVER_ERROR,
22        }
23    }
24}
25
26impl IntoResponse for ErrorResponse {
27    fn into_response(self) -> Response {
28        let body = Json(serde_json::json!({
29            "message": self.message,
30        }));
31
32        (self.status, body).into_response()
33    }
34}
35
36impl From<anyhow::Error> for ErrorResponse {
37    fn from(err: anyhow::Error) -> Self {
38        Self::internal(err.to_string())
39    }
40}
41
42impl From<ConfigError> for ErrorResponse {
43    fn from(err: ConfigError) -> Self {
44        Self::internal(err.to_string())
45    }
46}