soph_server/response/
error.rs

1use crate::error::Error;
2use axum::{
3    http::StatusCode,
4    response::{IntoResponse, Response},
5};
6
7impl IntoResponse for Error {
8    fn into_response(self) -> Response {
9        let (status, message) = match self {
10            #[cfg(feature = "request-auth")]
11            Self::Auth(error) => (StatusCode::UNAUTHORIZED, error.to_string()),
12            #[cfg(feature = "request-form")]
13            Self::FormRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
14            #[cfg(feature = "request-path")]
15            Self::PathRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
16            #[cfg(feature = "request-query")]
17            Self::QueryRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
18            #[cfg(feature = "request-json")]
19            Self::JsonRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
20            #[cfg(feature = "request-multipart")]
21            Self::MultipartRejection(error) => (StatusCode::BAD_REQUEST, error.to_string()),
22            #[cfg(feature = "request-multipart")]
23            Self::Multipart(error) => (StatusCode::BAD_REQUEST, error.to_string()),
24            #[cfg(feature = "request-validate")]
25            Self::Validate(error) => (StatusCode::BAD_REQUEST, format!("Validation error: {error}")),
26            #[cfg(feature = "database")]
27            Self::SeaOrm(error) => {
28                let status = match error {
29                    sea_orm::DbErr::RecordNotFound(_) => StatusCode::NOT_FOUND,
30                    _ => StatusCode::INTERNAL_SERVER_ERROR,
31                };
32
33                (status, error.to_string())
34            }
35            #[cfg(feature = "response-view")]
36            Self::Tera(error) => (StatusCode::INTERNAL_SERVER_ERROR, format!("View error: {error}")),
37            _ => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
38        };
39
40        tracing::warn!(error = message, "response");
41
42        #[cfg(not(feature = "response-json"))]
43        {
44            (status, message).into_response()
45        }
46
47        #[cfg(feature = "response-json")]
48        {
49            crate::response::json().code(status).message(message).into_response()
50        }
51    }
52}