mirror of https://github.com/rwf2/Rocket.git
Rename 'Rocket::catch()' to 'Rocket::register()'.
This commit is contained in:
parent
112e700836
commit
351757c6ee
|
@ -11,9 +11,8 @@ use yansi::Color::*;
|
|||
///
|
||||
/// 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 the [Rocket::catch](/rocket/struct.Rocket.html#method.catch)
|
||||
/// method. For example, to handle "404 not found" errors, a catcher for the
|
||||
/// "404" status code is registered.
|
||||
/// with Rocket via the [`Rocket::register()`] method. 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
|
||||
|
@ -54,7 +53,7 @@ use yansi::Color::*;
|
|||
///
|
||||
/// fn main() {
|
||||
/// # if false { // We don't actually want to launch the server in an example.
|
||||
/// rocket::ignite().catch(catchers![internal_error, not_found]).launch();
|
||||
/// rocket::ignite().register(catchers![internal_error, not_found]).launch();
|
||||
/// # }
|
||||
/// }
|
||||
/// ```
|
||||
|
|
|
@ -562,13 +562,14 @@ impl Rocket {
|
|||
///
|
||||
/// fn main() {
|
||||
/// # if false { // We don't actually want to launch the server in an example.
|
||||
/// rocket::ignite().catch(catchers![internal_error, not_found])
|
||||
/// rocket::ignite()
|
||||
/// .register(catchers![internal_error, not_found])
|
||||
/// # .launch();
|
||||
/// # }
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn catch(mut self, catchers: Vec<Catcher>) -> Self {
|
||||
pub fn register(mut self, catchers: Vec<Catcher>) -> Self {
|
||||
info!("{}{}:", Paint::masked("👾 "), Paint::purple("Catchers"));
|
||||
for c in catchers {
|
||||
if self.catchers.get(&c.code).map_or(false, |e| !e.is_default) {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
extern crate rocket;
|
||||
|
||||
use rocket::{catch, Request, response::Redirect};
|
||||
use rocket::{catch, response::Redirect};
|
||||
|
||||
#[catch(404)]
|
||||
fn not_found() -> Redirect {
|
||||
|
@ -17,7 +17,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn error_catcher_redirect() {
|
||||
let client = Client::new(rocket::ignite().catch(catchers![not_found])).unwrap();
|
||||
let client = Client::new(rocket::ignite().register(catchers![not_found])).unwrap();
|
||||
let response = client.get("/unknown").dispatch();
|
||||
println!("Response:\n{:?}", response);
|
||||
|
||||
|
|
|
@ -51,6 +51,6 @@ fn not_found(request: &Request) -> content::Html<String> {
|
|||
fn main() {
|
||||
rocket::ignite()
|
||||
.mount("/hello", routes![get_hello, post_hello])
|
||||
.catch(catchers![not_found])
|
||||
.register(catchers![not_found])
|
||||
.launch();
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ fn test<H>(method: Method, uri: &str, header: H, status: Status, body: String)
|
|||
{
|
||||
let rocket = rocket::ignite()
|
||||
.mount("/hello", routes![super::get_hello, super::post_hello])
|
||||
.catch(catchers![super::not_found]);
|
||||
.register(catchers![super::not_found]);
|
||||
|
||||
let client = Client::new(rocket).unwrap();
|
||||
let mut response = client.req(method, uri).header(header).dispatch();
|
||||
|
|
|
@ -23,7 +23,7 @@ fn main() {
|
|||
let e = rocket::ignite()
|
||||
// .mount("/", routes![hello, hello]) // uncoment this to get an error
|
||||
.mount("/", routes![hello])
|
||||
.catch(catchers![not_found])
|
||||
.register(catchers![not_found])
|
||||
.launch();
|
||||
|
||||
println!("Whoops! Rocket didn't launch!");
|
||||
|
|
|
@ -5,7 +5,7 @@ use rocket::http::Status;
|
|||
fn test(uri: &str, status: Status, body: String) {
|
||||
let rocket = rocket::ignite()
|
||||
.mount("/", routes![super::hello])
|
||||
.catch(catchers![super::not_found]);
|
||||
.register(catchers![super::not_found]);
|
||||
|
||||
let client = Client::new(rocket).unwrap();
|
||||
let mut response = client.get(uri).dispatch();
|
||||
|
|
|
@ -73,7 +73,7 @@ fn wow_helper(
|
|||
fn rocket() -> rocket::Rocket {
|
||||
rocket::ignite()
|
||||
.mount("/", routes![index, hello, about])
|
||||
.catch(catchers![not_found])
|
||||
.register(catchers![not_found])
|
||||
.attach(Template::custom(|engines| {
|
||||
engines.handlebars.register_helper("wow", Box::new(wow_helper));
|
||||
}))
|
||||
|
|
|
@ -72,7 +72,7 @@ fn not_found() -> JsonValue {
|
|||
fn rocket() -> rocket::Rocket {
|
||||
rocket::ignite()
|
||||
.mount("/message", routes![new, update, get])
|
||||
.catch(catchers![not_found])
|
||||
.register(catchers![not_found])
|
||||
.manage(Mutex::new(HashMap::<ID, String>::new()))
|
||||
}
|
||||
|
||||
|
|
|
@ -100,7 +100,7 @@ fn rocket() -> rocket::Rocket {
|
|||
.mount("/hello", vec![name.clone()])
|
||||
.mount("/hi", vec![name])
|
||||
.mount("/custom", CustomHandler::new("some data here"))
|
||||
.catch(vec![not_found_catcher])
|
||||
.register(vec![not_found_catcher])
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -42,7 +42,7 @@ fn rocket() -> rocket::Rocket {
|
|||
rocket::ignite()
|
||||
.mount("/", routes![index, get])
|
||||
.attach(Template::fairing())
|
||||
.catch(catchers![not_found])
|
||||
.register(catchers![not_found])
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -768,12 +768,13 @@ fn not_found(req: &Request) -> String {
|
|||
```
|
||||
|
||||
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:
|
||||
handle errors. The process, known as "registering" a catcher, is similar to
|
||||
mounting a route: call the `register` method with a list of catchers via the
|
||||
`catchers!` macro. The invocation to add the **404** catcher declared above
|
||||
looks like:
|
||||
|
||||
```rust
|
||||
rocket::ignite().catch(catchers![not_found])
|
||||
rocket::ignite().register(catchers![not_found])
|
||||
```
|
||||
|
||||
Unlike route request handlers, catchers take exactly zero or one parameters. If
|
||||
|
|
Loading…
Reference in New Issue