mirror of
https://github.com/tokio-rs/axum.git
synced 2025-01-11 12:31:25 +01:00
parent
9df57e6ff2
commit
0c18caa10f
3 changed files with 28 additions and 11 deletions
|
@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
# Unreleased
|
||||
|
||||
- None.
|
||||
- Add accessors to TypedHeaderRejection fields ([#317])
|
||||
|
||||
[#317]: https://github.com/tokio-rs/axum/pull/317
|
||||
|
||||
# 0.2.4 (10. September, 2021)
|
||||
|
||||
|
|
|
@ -333,4 +333,4 @@ where
|
|||
|
||||
#[cfg(feature = "headers")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "headers")))]
|
||||
pub use super::typed_header::TypedHeaderRejection;
|
||||
pub use super::typed_header::{TypedHeaderRejection, TypedHeaderRejectionReason};
|
||||
|
|
|
@ -48,7 +48,7 @@ where
|
|||
} else {
|
||||
return Err(TypedHeaderRejection {
|
||||
name: T::name(),
|
||||
reason: Reason::Missing,
|
||||
reason: TypedHeaderRejectionReason::Missing,
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -56,11 +56,11 @@ where
|
|||
Ok(Some(value)) => Ok(Self(value)),
|
||||
Ok(None) => Err(TypedHeaderRejection {
|
||||
name: T::name(),
|
||||
reason: Reason::Missing,
|
||||
reason: TypedHeaderRejectionReason::Missing,
|
||||
}),
|
||||
Err(err) => Err(TypedHeaderRejection {
|
||||
name: T::name(),
|
||||
reason: Reason::Error(err),
|
||||
reason: TypedHeaderRejectionReason::Error(err),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
@ -80,12 +80,27 @@ impl<T> Deref for TypedHeader<T> {
|
|||
#[derive(Debug)]
|
||||
pub struct TypedHeaderRejection {
|
||||
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)]
|
||||
enum Reason {
|
||||
pub enum TypedHeaderRejectionReason {
|
||||
/// The header was missing from the HTTP request
|
||||
Missing,
|
||||
/// An error occured when parsing the header from the HTTP request
|
||||
Error(headers::Error),
|
||||
}
|
||||
|
||||
|
@ -103,10 +118,10 @@ impl IntoResponse for TypedHeaderRejection {
|
|||
impl std::fmt::Display for TypedHeaderRejection {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match &self.reason {
|
||||
Reason::Missing => {
|
||||
TypedHeaderRejectionReason::Missing => {
|
||||
write!(f, "Header of type `{}` was missing", self.name)
|
||||
}
|
||||
Reason::Error(err) => {
|
||||
TypedHeaderRejectionReason::Error(err) => {
|
||||
write!(f, "{} ({})", err, self.name)
|
||||
}
|
||||
}
|
||||
|
@ -116,8 +131,8 @@ impl std::fmt::Display for TypedHeaderRejection {
|
|||
impl std::error::Error for TypedHeaderRejection {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match &self.reason {
|
||||
Reason::Error(err) => Some(err),
|
||||
Reason::Missing => None,
|
||||
TypedHeaderRejectionReason::Error(err) => Some(err),
|
||||
TypedHeaderRejectionReason::Missing => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue