Add accessors to TypedHeaderRejection fields (#317)

Fixes #316
This commit is contained in:
Olivier Pinon 2021-09-12 18:11:51 +02:00 committed by GitHub
parent 9df57e6ff2
commit 0c18caa10f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 11 deletions

View File

@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased # Unreleased
- None. - Add accessors to TypedHeaderRejection fields ([#317])
[#317]: https://github.com/tokio-rs/axum/pull/317
# 0.2.4 (10. September, 2021) # 0.2.4 (10. September, 2021)

View File

@ -333,4 +333,4 @@ where
#[cfg(feature = "headers")] #[cfg(feature = "headers")]
#[cfg_attr(docsrs, doc(cfg(feature = "headers")))] #[cfg_attr(docsrs, doc(cfg(feature = "headers")))]
pub use super::typed_header::TypedHeaderRejection; pub use super::typed_header::{TypedHeaderRejection, TypedHeaderRejectionReason};

View File

@ -48,7 +48,7 @@ where
} else { } else {
return Err(TypedHeaderRejection { return Err(TypedHeaderRejection {
name: T::name(), name: T::name(),
reason: Reason::Missing, reason: TypedHeaderRejectionReason::Missing,
}); });
}; };
@ -56,11 +56,11 @@ where
Ok(Some(value)) => Ok(Self(value)), Ok(Some(value)) => Ok(Self(value)),
Ok(None) => Err(TypedHeaderRejection { Ok(None) => Err(TypedHeaderRejection {
name: T::name(), name: T::name(),
reason: Reason::Missing, reason: TypedHeaderRejectionReason::Missing,
}), }),
Err(err) => Err(TypedHeaderRejection { Err(err) => Err(TypedHeaderRejection {
name: T::name(), name: T::name(),
reason: Reason::Error(err), reason: TypedHeaderRejectionReason::Error(err),
}), }),
} }
} }
@ -80,12 +80,27 @@ impl<T> Deref for TypedHeader<T> {
#[derive(Debug)] #[derive(Debug)]
pub struct TypedHeaderRejection { pub struct TypedHeaderRejection {
name: &'static http::header::HeaderName, name: &'static http::header::HeaderName,
reason: Reason, reason: TypedHeaderRejectionReason,
} }
impl TypedHeaderRejection {
/// Name of the header that caused the rejection
pub fn name(&self) -> &http::header::HeaderName {
self.name
}
/// Reason why the header extraction has failed
pub fn reason(&self) -> &TypedHeaderRejectionReason {
&self.reason
}
}
/// Additional information regarding a [`TypedHeaderRejection`](super::TypedHeaderRejection)
#[derive(Debug)] #[derive(Debug)]
enum Reason { pub enum TypedHeaderRejectionReason {
/// The header was missing from the HTTP request
Missing, Missing,
/// An error occured when parsing the header from the HTTP request
Error(headers::Error), Error(headers::Error),
} }
@ -103,10 +118,10 @@ impl IntoResponse for TypedHeaderRejection {
impl std::fmt::Display for TypedHeaderRejection { impl std::fmt::Display for TypedHeaderRejection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.reason { match &self.reason {
Reason::Missing => { TypedHeaderRejectionReason::Missing => {
write!(f, "Header of type `{}` was missing", self.name) write!(f, "Header of type `{}` was missing", self.name)
} }
Reason::Error(err) => { TypedHeaderRejectionReason::Error(err) => {
write!(f, "{} ({})", err, self.name) write!(f, "{} ({})", err, self.name)
} }
} }
@ -116,8 +131,8 @@ impl std::fmt::Display for TypedHeaderRejection {
impl std::error::Error for TypedHeaderRejection { impl std::error::Error for TypedHeaderRejection {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.reason { match &self.reason {
Reason::Error(err) => Some(err), TypedHeaderRejectionReason::Error(err) => Some(err),
Reason::Missing => None, TypedHeaderRejectionReason::Missing => None,
} }
} }
} }