Skip to content

Commit

Permalink
Add method to expose missing/invalid reject names
Browse files Browse the repository at this point in the history
Add a name method to `MissingHeader`, `InvalidHeader` and
`MissingCookie` that allow access to their internal name field. This is
useful when recovering from rejections and down-casting to return
specific replies for cases where additional information should be sent
to the client.
  • Loading branch information
johnchildren committed Feb 17, 2020
1 parent b79d489 commit 3315d41
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/reject.rs
Expand Up @@ -506,6 +506,13 @@ pub struct MissingHeader {
name: &'static str,
}

impl MissingHeader {
/// Retrieve the name of the header that was missing
pub fn name<'a>(self) -> &'a str {
self.name
}
}

impl ::std::fmt::Display for MissingHeader {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "Missing request header {:?}", self.name)
Expand All @@ -520,6 +527,13 @@ pub struct InvalidHeader {
name: &'static str,
}

impl InvalidHeader {
/// Retrieve the name of the header that was invalid
pub fn name<'a>(self) -> &'a str {
self.name
}
}

impl ::std::fmt::Display for InvalidHeader {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "Invalid request header {:?}", self.name)
Expand All @@ -534,6 +548,13 @@ pub struct MissingCookie {
name: &'static str,
}

impl MissingCookie {
/// Retrieve the name of the cookie that was missing
pub fn name<'a>(self) -> &'a str {
self.name
}
}

impl ::std::fmt::Display for MissingCookie {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "Missing request cookie {:?}", self.name)
Expand Down

0 comments on commit 3315d41

Please sign in to comment.