Skip to main content

Module error

Module error 

Source
Expand description

§modo::error

HTTP-aware error type for the modo web framework.

Error carries an HTTP status code, a human-readable message, an optional structured details payload, an optional boxed source error, an optional static machine-readable error_code, and an optional i18n locale_key. It implements axum::response::IntoResponse, so async fn handlers can return Result<T> and use ? everywhere.

§Provides

§Quick start

use modo::error::{Error, Result};

fn find_user(id: u64) -> Result<String> {
    if id == 0 {
        return Err(Error::not_found("user not found"));
    }
    Ok("Alice".to_string())
}

§Source drops on clone and response — use error_code for identity

The boxed source field is Box<dyn std::error::Error + Send + Sync>, which is not Clone. Both Clone and [Error::into_response] therefore drop the source. This means:

  • Pre-response (inside a handler or middleware that still owns the Error) — use Error::source_as::<T> to downcast the source to a concrete type.
  • Post-response (middleware that reads the error copy stored in response extensions) — the source is gone; use Error::error_code to recover the identity of the error. Attach it up front with Error::with_code.

A typical pattern is .chain(e).with_code(e.code()) so you keep both the causal source (inspectable pre-response) and the static code (stable post-response).

use modo::error::Error;

let err = Error::unauthorized("token expired").with_code("jwt:expired");
assert_eq!(err.error_code(), Some("jwt:expired"));

§Usage in middleware and guards

Middleware and route guards that need to short-circuit with an error should build an Error and call IntoResponse::into_response — never construct raw axum::response::Response values by hand. This ensures the JSON body shape and response-extension copy stay consistent across the framework.

Structs§

Error
The primary error type for the modo framework.

Enums§

HttpError
A lightweight enum of common HTTP error statuses.

Type Aliases§

Result
A type alias for std::result::Result<T, Error>.