postcrate_core/http/
error.rs1use axum::http::StatusCode;
5use axum::response::{IntoResponse, Response};
6use axum::Json;
7use serde::Serialize;
8
9use crate::error::Error;
10
11#[derive(Serialize)]
12struct ErrorBody<'a> {
13 error: &'a str,
14 message: String,
15}
16
17impl IntoResponse for Error {
18 fn into_response(self) -> Response {
19 if self.http_status().as_u16() >= 500 {
22 tracing::warn!(target: "postcrate::http", error = %self, code = self.code());
23 }
24 let status = self.http_status();
25 let body = ErrorBody {
26 error: self.code(),
27 message: self.to_string(),
28 };
29 let status = if matches!(status, StatusCode::OK) {
32 StatusCode::INTERNAL_SERVER_ERROR
33 } else {
34 status
35 };
36 (status, Json(body)).into_response()
37 }
38}