use axum::Json;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use serde::Serialize;
use utoipa::ToSchema;
use yorishiro_core::YorishiroError;
use yorishiro_core::error::ValidationDetail;
pub struct ApiError(pub YorishiroError);
impl From<YorishiroError> for ApiError {
fn from(err: YorishiroError) -> Self {
Self(err)
}
}
impl IntoResponse for ApiError {
fn into_response(self) -> Response {
let (status_u16, body) = self.0.into_http_parts();
let status = StatusCode::from_u16(status_u16).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
(status, Json(body)).into_response()
}
}
#[derive(Serialize, ToSchema)]
pub struct ApiErrorBody {
pub error: ApiErrorDetail,
}
#[derive(Serialize, ToSchema)]
pub struct ApiErrorDetail {
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub details: Option<Vec<ValidationDetail>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub hint: Option<String>,
}