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;

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

§Grammar

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

catch := STATUS

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

§Typing Requirements

The decorated function must take exactly zero or one argument. If the decorated function takes an argument, the argument’s type must be &Request.

The return type of the decorated function must implement the Responder trait.

§Semantics

The attribute generates two items:

  1. An ErrorHandler.

    The generated handler calls the decorated function, passing in the &Request value 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. The handler is set to the generated handler.