rocket

Attribute Macro catch

Source
#[catch]
Expand description

Attribute to generate a Catcher and associated metadata.

This attribute can only be applied to free functions:

use rocket::Request;
use rocket::http::Status;

#[catch(404)]
fn not_found(req: &Request) -> String {
    format!("Sorry, {} does not exist.", req.uri())
}

#[catch(default)]
fn default(status: Status, req: &Request) -> String {
    format!("{} ({})", status, req.uri())
}

§Grammar

The grammar for the #[catch] attributes is defined as:

catch := STATUS | 'default'

STATUS := valid HTTP status code (integer in [200, 599])

§Typing Requirements

The decorated function may take zero, one, or two arguments. It’s type signature must be one of the following, where R:Responder:

§Semantics

The attribute generates two items:

  1. An error Handler.

    The generated handler calls the decorated function, passing in the Status and &Request values if requested. The returned value is used to generate a Response via the type’s Responder implementation.

  2. A static structure used by catchers! to generate a Catcher.

    The static structure (and resulting Catcher) is populated with the name (the function’s name) and status code from the route attribute or None if default. The handler is set to the generated handler.