ayun_server/response/
error.rs1use 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) => (
26 StatusCode::BAD_REQUEST,
27 format!("Validation error: {}", error),
28 ),
29 #[cfg(feature = "database")]
30 Self::SeaOrm(error) => {
31 let status = match error {
32 sea_orm::DbErr::RecordNotFound(_) => StatusCode::NOT_FOUND,
33 _ => StatusCode::INTERNAL_SERVER_ERROR,
34 };
35
36 (status, error.to_string())
37 }
38 #[cfg(feature = "response-view")]
39 Self::Tera(error) => (
40 StatusCode::INTERNAL_SERVER_ERROR,
41 format!("View error: {error}"),
42 ),
43 _ => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
44 };
45
46 tracing::warn!(error = message, "response");
47
48 #[cfg(not(feature = "response-json"))]
49 {
50 (status, message).into_response()
51 }
52
53 #[cfg(feature = "response-json")]
54 {
55 crate::response::json()
56 .code(status)
57 .message(message)
58 .into_response()
59 }
60 }
61}