From 9d06cd2c2e5cca73128555a308fc5c63d427700a Mon Sep 17 00:00:00 2001 From: John Children Date: Mon, 17 Feb 2020 09:28:49 +0000 Subject: [PATCH] Add method to expose missing/invalid reject names 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. --- src/reject.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/reject.rs b/src/reject.rs index e05535a97..d0d890533 100644 --- a/src/reject.rs +++ b/src/reject.rs @@ -506,6 +506,13 @@ pub struct MissingHeader { name: &'static str, } +impl MissingHeader { + /// Retrieve the name of the header that was missing + pub fn name(self) -> &'static 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) @@ -520,6 +527,13 @@ pub struct InvalidHeader { name: &'static str, } +impl InvalidHeader { + /// Retrieve the name of the header that was invalid + pub fn name(self) -> &'static 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) @@ -534,6 +548,13 @@ pub struct MissingCookie { name: &'static str, } +impl MissingCookie { + /// Retrieve the name of the cookie that was missing + pub fn name(self) -> &'static 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)