Error Catchers

Routing may fail for a variety of reasons. These include:

If any of these conditions occur, Rocket returns an error to the client. To do so, Rocket invokes the error catcher corresponding to the error’s status code. A catcher is like a route, except it only handles errors. Catchers are declared via the catch attribute, which takes a single integer corresponding to the HTTP status code to catch. For instance, to declare a catcher for 404 errors, you’d write:

  1. #[catch(404)]
  2. fn not_found(req: &Request) -> String { ... }

As with routes, Rocket needs to know about a catcher before it is used to handle errors. The process is similar to mounting: call the catch method with a list of catchers via the catchers! macro. The invocation to add the 404 catcher declared above looks like:

  1. rocket::ignite().catch(catchers![not_found])

Unlike request handlers, error handlers can only take 0, 1, or 2 parameters of types Request and/or Error. At present, the Error type is not particularly useful, and so it is often omitted. The error catcher example on GitHub illustrates their use in full.

Rocket has a default catcher for all of the standard HTTP error codes including 404, 500, and more.