diff --git a/core/codegen/src/attribute/catch/mod.rs b/core/codegen/src/attribute/catch/mod.rs index 11ae3597..6ae4718c 100644 --- a/core/codegen/src/attribute/catch/mod.rs +++ b/core/codegen/src/attribute/catch/mod.rs @@ -78,6 +78,7 @@ pub fn _catch( } #StaticCatcherInfo { + name: stringify!(#user_catcher_fn_name), code: #status_code, handler: monomorphized_function, } diff --git a/core/lib/src/catcher.rs b/core/lib/src/catcher.rs index 62ffebba..b7cdf0ad 100644 --- a/core/lib/src/catcher.rs +++ b/core/lib/src/catcher.rs @@ -101,6 +101,9 @@ pub type ErrorHandlerFuture<'r> = BoxFuture<'r, Result<'r>>; /// [`Status`]: crate::http::Status #[derive(Clone)] pub struct Catcher { + /// The name of this catcher, if one was given. + pub name: Option>, + /// The HTTP status code to match against if this route is not `default`. pub code: Option, @@ -143,7 +146,7 @@ impl Catcher { pub fn new(code: C, handler: H) -> Catcher where C: Into>, 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)) }) } - Catcher { code: None, handler: Box::new(async_default) } + let name = Some("".into()); + Catcher { name, code: None, handler: Box::new(async_default) } } } @@ -272,12 +276,18 @@ impl ErrorHandler for F impl From for Catcher { #[inline] 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 { 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 { Some(code) => write!(f, "{}", Paint::blue(code)), None => write!(f, "{}", Paint::blue("default")) diff --git a/core/lib/src/codegen.rs b/core/lib/src/codegen.rs index 085823c7..943701b0 100644 --- a/core/lib/src/codegen.rs +++ b/core/lib/src/codegen.rs @@ -27,6 +27,8 @@ pub struct StaticRouteInfo { /// Information generated by the `catch` attribute during codegen. pub struct StaticCatcherInfo { + /// The catcher's name, i.e, the name of the function. + pub name: &'static str, /// The catcher's status code. pub code: Option, /// The catcher's handler, i.e, the annotated function. diff --git a/core/lib/src/server.rs b/core/lib/src/server.rs index e1a0b882..e9e20f15 100644 --- a/core/lib/src/server.rs +++ b/core/lib/src/server.rs @@ -336,7 +336,8 @@ impl Rocket { if let Some(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)) .unwrap_or_else(|| Err(None)) } else {