-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Catch and gracefully handle panics in routes and catchers.
- Loading branch information
Showing
2 changed files
with
56 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#[macro_use] extern crate rocket; | ||
|
||
use rocket::Rocket; | ||
use rocket::http::Status; | ||
use rocket::local::blocking::Client; | ||
|
||
#[get("/panic")] | ||
fn panic_route() -> &'static str { | ||
panic!("Panic in route") | ||
} | ||
|
||
#[catch(404)] | ||
fn panic_catcher() -> &'static str { | ||
panic!("Panic in catcher") | ||
} | ||
|
||
fn rocket() -> Rocket { | ||
rocket::ignite() | ||
.mount("/", routes![panic_route]) | ||
.register(catchers![panic_catcher]) | ||
} | ||
|
||
#[test] | ||
fn catches_route_panic() { | ||
let client = Client::tracked(rocket()).unwrap(); | ||
let response = client.get("/panic").dispatch(); | ||
assert_eq!(response.status(), Status::InternalServerError); | ||
|
||
} | ||
|
||
#[test] | ||
fn catches_catcher_panic() { | ||
let client = Client::tracked(rocket()).unwrap(); | ||
let response = client.get("/noroute").dispatch(); | ||
assert_eq!(response.status(), Status::InternalServerError); | ||
} |