Track and log catcher names.

This commit is contained in:
Sergio Benitez 2021-03-15 02:27:53 -07:00
parent 304e65ac72
commit 9c678e606b
4 changed files with 18 additions and 4 deletions

View File

@ -78,6 +78,7 @@ pub fn _catch(
} }
#StaticCatcherInfo { #StaticCatcherInfo {
name: stringify!(#user_catcher_fn_name),
code: #status_code, code: #status_code,
handler: monomorphized_function, handler: monomorphized_function,
} }

View File

@ -101,6 +101,9 @@ pub type ErrorHandlerFuture<'r> = BoxFuture<'r, Result<'r>>;
/// [`Status`]: crate::http::Status /// [`Status`]: crate::http::Status
#[derive(Clone)] #[derive(Clone)]
pub struct Catcher { pub struct Catcher {
/// The name of this catcher, if one was given.
pub name: Option<Cow<'static, str>>,
/// The HTTP status code to match against if this route is not `default`. /// The HTTP status code to match against if this route is not `default`.
pub code: Option<u16>, pub code: Option<u16>,
@ -143,7 +146,7 @@ impl Catcher {
pub fn new<C, H>(code: C, handler: H) -> Catcher pub fn new<C, H>(code: C, handler: H) -> Catcher
where C: Into<Option<u16>>, H: ErrorHandler where C: Into<Option<u16>>, H: ErrorHandler
{ {
Catcher { code: code.into(), handler: Box::new(handler) } Catcher { name: None, code: code.into(), handler: Box::new(handler) }
} }
} }
@ -153,7 +156,8 @@ impl Default for Catcher {
Box::pin(async move { Ok(default(status, request)) }) Box::pin(async move { Ok(default(status, request)) })
} }
Catcher { code: None, handler: Box::new(async_default) } let name = Some("<Rocket Catcher>".into());
Catcher { name, code: None, handler: Box::new(async_default) }
} }
} }
@ -272,12 +276,18 @@ impl<F: Clone + Sync + Send + 'static> ErrorHandler for F
impl From<StaticCatcherInfo> for Catcher { impl From<StaticCatcherInfo> for Catcher {
#[inline] #[inline]
fn from(info: StaticCatcherInfo) -> Catcher { fn from(info: StaticCatcherInfo) -> Catcher {
Catcher::new(info.code, info.handler) let mut catcher = Catcher::new(info.code, info.handler);
catcher.name = Some(info.name.into());
catcher
} }
} }
impl fmt::Display for Catcher { impl fmt::Display for Catcher {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ref n) = self.name {
write!(f, "{}{}{} ", Paint::cyan("("), Paint::white(n), Paint::cyan(")"))?;
}
match self.code { match self.code {
Some(code) => write!(f, "{}", Paint::blue(code)), Some(code) => write!(f, "{}", Paint::blue(code)),
None => write!(f, "{}", Paint::blue("default")) None => write!(f, "{}", Paint::blue("default"))

View File

@ -27,6 +27,8 @@ pub struct StaticRouteInfo {
/// Information generated by the `catch` attribute during codegen. /// Information generated by the `catch` attribute during codegen.
pub struct StaticCatcherInfo { pub struct StaticCatcherInfo {
/// The catcher's name, i.e, the name of the function.
pub name: &'static str,
/// The catcher's status code. /// The catcher's status code.
pub code: Option<u16>, pub code: Option<u16>,
/// The catcher's handler, i.e, the annotated function. /// The catcher's handler, i.e, the annotated function.

View File

@ -336,7 +336,8 @@ impl Rocket {
if let Some(catcher) = catcher { if let Some(catcher) = catcher {
warn_!("Responding with registered {} catcher.", catcher); warn_!("Responding with registered {} catcher.", catcher);
handle(None, || catcher.handler.handle(status, req)).await let name = catcher.name.as_deref();
handle(name, || catcher.handler.handle(status, req)).await
.map(|result| result.map_err(Some)) .map(|result| result.map_err(Some))
.unwrap_or_else(|| Err(None)) .unwrap_or_else(|| Err(None))
} else { } else {