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 52 53 54 55 56
use std::sync::atomic::{AtomicUsize, Ordering};
use afire::{Content, Method, Response, Server, Status};
use crate::Example;
// You can run this example with `cargo run --example basic -- error_handling`
// Don't crash thread from a panic in a route
// This does not apply to the error handler itself
// afire will catch any panic in a route and return a 500 error by default
pub struct ErrorHandling;
impl Example for ErrorHandling {
fn name(&self) -> &'static str {
"error_handling"
}
fn exec(&self) {
// Create a new Server instance on localhost port 8080
let mut server = Server::<()>::new("localhost", 8080);
// Define a route that will panic
server.route(Method::GET, "/panic", |_req| panic!("This is a panic!"));
// Give the server a main page
server.route(Method::GET, "/", |_req| {
Response::new()
.text(r#"<a href="/panic">PANIC</a>"#)
.content(Content::HTML)
});
// You can optionally define a custom error handler
// This can be defined anywhere in the server and will take affect for all routes
// Its like a normal route, but it will only be called if the route panics
let errors = AtomicUsize::new(1);
server.error_handler(move |_state, _req, err| {
Response::new()
.status(Status::InternalServerError)
.text(format!(
"<h1>Internal Server Error #{}</h1><br>Panicked at '{}'",
errors.fetch_add(1, Ordering::Relaxed),
err
))
.content(Content::HTML)
});
// You can now goto http://localhost:8080/panic
// This will cause the route to panic and return a 500 error
// Start the server
// This will block the current thread
server.start().unwrap();
}
}