pub trait ExceptionHandler:
Send
+ Sync
+ 'static {
// Required method
fn handle_exception<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
error: Error,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
}native and crate feature core only.Expand description
A strategy for turning a dispatch error into an HTTP response.
Install an implementation with with_exception_handler on the router, the
server, or a middleware chain. Every error the framework produces while
serving a request then flows through this method instead of the default
impl From<Error> for Response conversion.
§Responsibility
Installing a handler transfers responsibility for the response body and
headers to the handler. The default conversion never exposes internal
details and returns JSON with Content-Type: application/json; server
errors use a generic category and client errors may include a safe detail.
A custom handler owns the response headers and provides none of these
guarantees unless it sets them itself. Interpolating Display output of
the error into a response body can disclose internal paths and credentials.
§Panics
A panicking handler is not caught here. The request’s connection task fails and the server process stays alive.
§Examples
use async_trait::async_trait;
use hyper::StatusCode;
use reinhardt_http::{Error, ExceptionHandler, Request, Response};
struct JsonErrors;
#[async_trait]
impl ExceptionHandler for JsonErrors {
async fn handle_exception(&self, _request: &Request, error: Error) -> Response {
let status = StatusCode::from_u16(error.status_code())
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
Response::new(status).with_body("error")
}
}Required Methods§
Sourcefn handle_exception<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
error: Error,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn handle_exception<'life0, 'life1, 'async_trait>(
&'life0 self,
request: &'life1 Request,
error: Error,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Builds the response for error.
request carries the method, URI, version, headers, path parameters,
query parameters and extensions of the original request, which is
exactly what Request::clone_for_di preserves. Its body is empty
because the original request body has already been consumed by the
handler that produced the error.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".