Skip to main content

error_handler

Function error_handler 

Source
pub fn error_handler<F, Fut>(handler: F) -> ErrorHandlerLayer<F>
where F: Fn(Error, Parts) -> Fut + Clone + Send + Sync + 'static, Fut: Future<Output = Response> + Send + 'static,
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));