pub fn error_handler<F, Fut>(handler: F) -> ErrorHandlerLayer<F>Expand description
Creates an error-handler layer that intercepts responses containing a
crate::error::Error in their extensions and rewrites them through
the supplied handler function.
Any middleware that stores a modo::Error in response extensions
(Error::into_response(), catch_panic, csrf, rate_limit, etc.)
will be caught by this layer, giving the application a single place to
control the error response format (JSON, HTML, plain text, etc.).
The handler receives the error and the original request parts (method, URI, headers, extensions) by value.
ยงExample
use axum::{Router, routing::get};
use axum::response::IntoResponse;
async fn render_error(err: modo::Error, parts: http::request::Parts) -> axum::response::Response {
(err.status(), err.message().to_string()).into_response()
}
let app: Router = Router::new()
.route("/", get(|| async { "ok" }))
.layer(modo::middleware::error_handler(render_error));