1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use garde::Report;
use thiserror::Error;

/// Rejection used for [`WithValidation`]
///
/// [`WithValidation`]: crate::WithValidation
#[derive(Debug, Error)]
pub enum WithValidationRejection<T> {
    /// Variant for the extractor's rejection
    #[error(transparent)]
    ExtractionError(T),

    /// Variant for the payload's validation errors. Responds with status code
    /// `422 Unprocessable Content`
    #[error(transparent)]
    ValidationError(#[from] Report),
}

impl<T: IntoResponse> IntoResponse for WithValidationRejection<T> {
    fn into_response(self) -> Response {
        match self {
            WithValidationRejection::ExtractionError(t) => t.into_response(),
            WithValidationRejection::ValidationError(e) => {
                (StatusCode::UNPROCESSABLE_ENTITY, format!("{e}")).into_response()
            }
        }
    }
}