From 1a349287bccb372d33179eb7ebb11ec4208131c9 Mon Sep 17 00:00:00 2001 From: gengteng Date: Sat, 23 Sep 2023 22:45:20 +0800 Subject: [PATCH] impl Error for ValidRejection --- src/lib.rs | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f6abfe2..cebbaec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -137,30 +137,36 @@ impl From for ValidError { } } -impl IntoResponse for ValidError { - fn into_response(self) -> Response { - match self { - ValidError::Valid(validate_error) => { - #[cfg(feature = "into_json")] - { - (VALIDATION_ERROR_STATUS, axum::Json(validate_error)).into_response() - } - #[cfg(not(feature = "into_json"))] - { - (VALIDATION_ERROR_STATUS, validate_error.to_string()).into_response() - } - } - ValidError::Inner(json_error) => json_error.into_response(), - } - } -} - /// Validation Rejection pub struct ValidRejection { error: ValidError, response_builder: fn(ValidationErrors) -> Response, } +impl Display for ValidRejection { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match &self.error { + ValidError::Valid(errors) => write!(f, "{errors}"), + ValidError::Inner(error) => write!(f, "{error}"), + } + } +} + +impl Debug for ValidRejection { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + Debug::fmt(&self.error, f) + } +} + +impl Error for ValidRejection { + fn source(&self) -> Option<&(dyn Error + 'static)> { + match &self.error { + ValidError::Valid(ve) => Some(ve), + ValidError::Inner(e) => Some(e), + } + } +} + impl IntoResponse for ValidRejection { fn into_response(self) -> Response { match self.error {