Struct rocket::Catcher

source ·
pub struct Catcher {
    pub code: u16,
    pub handler: ErrorHandler,
    /* private fields */
}
Expand description

An error catching route.

Catchers are routes that run when errors occur. They correspond directly with the HTTP error status code they will be handling and are registered with Rocket via Rocket::register(). For example, to handle “404 not found” errors, a catcher for the “404” status code is registered.

Because error handlers are only called when all routes are exhausted, they should not fail nor forward. If an error catcher fails, the user will receive no response. If an error catcher forwards, Rocket will respond with an internal server error.

§Built-In Catchers

Rocket has many built-in, pre-registered default catchers. In particular, Rocket has catchers for all of the following status codes: 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 426, 428, 429, 431, 451, 500, 501, 503, and 510. As such, catchers only need to be registered if an error needs to be handled in a custom fashion.

§Code Generation

Catchers should rarely be used directly. Instead, they are typically declared using the catch decorator, as follows:

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

use rocket::Request;

#[catch(500)]
fn internal_error() -> &'static str {
    "Whoops! Looks like we messed up."
}

#[catch(404)]
fn not_found(req: &Request) -> String {
    format!("I couldn't find '{}'. Try something else?", req.uri())
}

fn main() {
    rocket::ignite().register(catchers![internal_error, not_found]).launch();
}

A function decorated with catch must take exactly zero or one arguments. If the catcher takes an argument, it must be of type &Request.

Fields§

§code: u16

The HTTP status code to match against.

§handler: ErrorHandler

The catcher’s associated handler.

Implementations§

source§

impl Catcher

source

pub fn new(code: u16, handler: ErrorHandler) -> Catcher

Creates a catcher for the given status code using the given error handler. This should only be used when routing manually.

§Examples
use rocket::{Catcher, Request};
use rocket::response::{Result, Responder};
use rocket::response::status::Custom;
use rocket::http::Status;

fn handle_404<'r>(req: &'r Request) -> Result<'r> {
    let res = Custom(Status::NotFound, format!("404: {}", req.uri()));
    res.respond_to(req)
}

fn handle_500<'r>(req: &'r Request) -> Result<'r> {
    "Whoops, we messed up!".respond_to(req)
}

let not_found_catcher = Catcher::new(404, handle_404);
let internal_server_error_catcher = Catcher::new(500, handle_500);

Trait Implementations§

source§

impl Display for Catcher

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T, I> AsResult<T, I> for T
where I: Input,

source§

fn as_result(self) -> Result<T, ParseErr<I>>

source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> IntoCollection<T> for T

§

fn into_collection<A>(self) -> SmallVec<A>
where A: Array<Item = T>,

Converts self into a collection.
§

fn mapped<U, F, A>(self, f: F) -> SmallVec<A>
where F: FnMut(T) -> U, A: Array<Item = U>,

source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> Typeable for T
where T: Any,

source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V