Type Definition humphrey::app::ErrorHandler[][src]

pub type ErrorHandler = fn(_: Option<Request>, _: StatusCode) -> Response;
Expand description

Represents a function able to handle an error. The first parameter of type Option<Request> will be Some if the request could be parsed. Otherwise, it will be None and the status code will be StatusCode::BadRequest.

Every app has a default error handler, which simply displays the status code. The source code for this default error handler is copied below since it is a good example.

Example

fn error_handler(request: Option<Request>, status_code: StatusCode) -> Response {
    let body = format!(
        "<html><body><h1>{} {}</h1></body></html>",
        Into::<u16>::into(status_code.clone()),
        Into::<&str>::into(status_code.clone())
    );
     
    if let Some(request) = request {
        Response::new(status_code, body.as_bytes(), &request)
    } else {
        Response::empty(status_code)
            .with_bytes(body.as_bytes())
            .with_generated_headers()
    }
}