use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use serde::Serialize;
use crate::error::Error;
#[derive(Serialize)]
struct ErrorBody<'a> {
error: &'a str,
message: String,
}
impl IntoResponse for Error {
fn into_response(self) -> Response {
if self.http_status().as_u16() >= 500 {
tracing::warn!(target: "postcrate::http", error = %self, code = self.code());
}
let status = self.http_status();
let body = ErrorBody {
error: self.code(),
message: self.to_string(),
};
let status = if matches!(status, StatusCode::OK) {
StatusCode::INTERNAL_SERVER_ERROR
} else {
status
};
(status, Json(body)).into_response()
}
}