mirror of
https://github.com/rwf2/Rocket.git
synced 2025-02-22 00:22:04 +00:00
Improve error catcher documentation in requests guide.
This commit is contained in:
parent
070529e1ac
commit
0842612607
@ -742,24 +742,34 @@ errors, you'd write:
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
#[catch(404)]
|
#[catch(404)]
|
||||||
fn not_found(req: &Request) -> String { ... }
|
fn not_found(req: &Request) -> T { .. }
|
||||||
```
|
```
|
||||||
|
|
||||||
As with routes, Rocket needs to know about a catcher before it is used to handle
|
As with routes, the return type (here `T`) must implement `Responder`. A
|
||||||
errors. The process is similar to mounting: call the `catch` method with a list
|
concrete implementation may look like:
|
||||||
of catchers via the `catchers!` macro. The invocation to add the **404** catcher
|
|
||||||
declared above looks like:
|
```rust
|
||||||
|
#[catch(404)]
|
||||||
|
fn not_found(req: &Request) -> String {
|
||||||
|
format!("Sorry, '{}' is not a valid path.", req.uri())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Also 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:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
rocket::ignite().catch(catchers![not_found])
|
rocket::ignite().catch(catchers![not_found])
|
||||||
```
|
```
|
||||||
|
|
||||||
Unlike request handlers, error handlers can only take 0, 1, or 2 parameters of
|
Unlike route request handlers, catchers can only take 0, 1, or 2 parameters of
|
||||||
types [`Request`](https://api.rocket.rs/rocket/struct.Request.html) and/or
|
types [`Request`](https://api.rocket.rs/rocket/struct.Request.html) and/or
|
||||||
[`Error`](https://api.rocket.rs/rocket/enum.Error.html). At present, the `Error`
|
[`Error`](https://api.rocket.rs/rocket/enum.Error.html). At present, the `Error`
|
||||||
type is not particularly useful, and so it is often omitted. The [error catcher
|
type is not particularly useful, and so it is often omitted. The [error catcher
|
||||||
example](https://github.com/SergioBenitez/Rocket/tree/v0.4.0-dev/examples/errors) on
|
example](https://github.com/SergioBenitez/Rocket/tree/v0.4.0-dev/examples/errors)
|
||||||
GitHub illustrates their use in full.
|
on GitHub illustrates their use in full.
|
||||||
|
|
||||||
Rocket has a default catcher for all of the standard HTTP error codes including
|
Rocket has a default catcher for all of the standard HTTP error codes including
|
||||||
**404**, **500**, and more.
|
**404**, **500**, and more.
|
||||||
|
Loading…
Reference in New Issue
Block a user