From 0c18caa10f5d67800874a11b8fa0e24a392291cb Mon Sep 17 00:00:00 2001 From: Olivier Pinon Date: Sun, 12 Sep 2021 18:11:51 +0200 Subject: [PATCH] Add accessors to `TypedHeaderRejection` fields (#317) Fixes #316 --- CHANGELOG.md | 4 +++- src/extract/rejection.rs | 2 +- src/extract/typed_header.rs | 33 ++++++++++++++++++++++++--------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddb75170..519f96bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/extract/rejection.rs b/src/extract/rejection.rs index d8632e70..6a17f1dc 100644 --- a/src/extract/rejection.rs +++ b/src/extract/rejection.rs @@ -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}; diff --git a/src/extract/typed_header.rs b/src/extract/typed_header.rs index 40b5c673..276ea208 100644 --- a/src/extract/typed_header.rs +++ b/src/extract/typed_header.rs @@ -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 Deref for TypedHeader { #[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, } } }