Skip to main content

default_error_handler

Function default_error_handler 

Source
pub async fn default_error_handler(err: Error, parts: Parts) -> Response
Expand description

Default error responder suitable for passing directly to error_handler.

Produces the same JSON shape as crate::Error::into_response:

{ "error": { "status": 404, "message": "..." } }

When the error carries a translation key (via Error::localized or Error::with_locale_key) and the request has a Translator in its extensions (typically injected by I18nLayer), the key is resolved at response-build time and the translated string is used as the response message. Otherwise the error’s stored message is used unchanged.

§Layer ordering

When pairing with I18nLayer, install I18nLayer outside error_handler (apply i18n.layer() after error_handler in .layer(...) calls) so the Translator is inserted into the request extensions before error_handler clones the request parts. Reversed ordering silently falls back to the raw key.

§Example

use axum::{Router, routing::get};
use modo::middleware::{default_error_handler, error_handler};

let app: Router = Router::new()
    .route("/", get(|| async { "ok" }))
    .layer(error_handler(default_error_handler))
    .layer(i18n.layer());  // outer — must run before error_handler