Add error implementation for ValidRejection

This commit is contained in:
Rick van der Wal
2023-08-04 17:20:16 +02:00
parent 58d48b66b3
commit 42ed7c7dc7

View File

@@ -19,6 +19,8 @@ pub mod typed_header;
#[cfg(feature = "yaml")] #[cfg(feature = "yaml")]
pub mod yaml; pub mod yaml;
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use axum::async_trait; use axum::async_trait;
use axum::extract::{FromRequest, FromRequestParts}; use axum::extract::{FromRequest, FromRequestParts};
use axum::http::request::Parts; use axum::http::request::Parts;
@@ -54,6 +56,7 @@ impl<E> DerefMut for Valid<E> {
/// If the valid extractor fails it'll use this "rejection" type. /// If the valid extractor fails it'll use this "rejection" type.
/// This rejection type can be converted into a response. /// This rejection type can be converted into a response.
#[derive(Debug)]
pub enum ValidRejection<E> { pub enum ValidRejection<E> {
/// Validation errors /// Validation errors
Valid(ValidationErrors), Valid(ValidationErrors),
@@ -61,6 +64,17 @@ pub enum ValidRejection<E> {
Inner(E), Inner(E),
} }
impl<E: Display> Display for ValidRejection<E> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ValidRejection::Valid(errors) => write!(f, "{errors}"),
ValidRejection::Inner(error) => write!(f, "{error}"),
}
}
}
impl<E: Debug + Display> Error for ValidRejection<E> {}
impl<E> From<ValidationErrors> for ValidRejection<E> { impl<E> From<ValidationErrors> for ValidRejection<E> {
fn from(value: ValidationErrors) -> Self { fn from(value: ValidationErrors) -> Self {
Self::Valid(value) Self::Valid(value)