-
Notifications
You must be signed in to change notification settings - Fork 157
/
custom_error_handler.rs
51 lines (42 loc) · 1.7 KB
/
custom_error_handler.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#[macro_use] extern crate nickel;
use nickel::{Nickel, NickelError, Request, HttpRouter, Action};
use nickel::status::StatusCode;
#[tokio::main]
async fn main() {
let mut server = Nickel::new();
// go to http://localhost:6767/user/4711 to see this route in action
server.get("/user/:userid", middleware! { |request|
if let Some("42") = request.param("userid") {
(StatusCode::OK, "User 42 was found!")
} else {
(StatusCode::IM_A_TEAPOT, "Teapot activated!")
}
});
//this is how to overwrite the default error handler to handle 404 cases with a custom view
fn custom_handler<D: Send + 'static + Sync>(err: &mut NickelError<D>, req: &mut Request<D>) -> Action {
// Print the internal error message and path to the console
println!("[{}] ERROR: {}",
req.path_without_query(),
err.message);
if let Some(ref mut res) = err.stream {
match res.status() {
StatusCode::IM_A_TEAPOT => {
// Pass the internal message to the client
let _ = res.set_body(err.message.clone());
return Action::Halt(())
}
StatusCode::NOT_FOUND => {
let _ = res.set_body("<h1>404 - Not Found</h1>");
return Action::Halt(())
}
_ => {}
}
}
// Fall through to next error handler
Action::Continue(())
}
// issue #20178
let custom_handler: fn(&mut NickelError<()>, &mut Request<()>) -> Action = custom_handler;
server.handle_error(custom_handler);
server.listen("127.0.0.1:6767").await.unwrap();
}