Skip to main content

modo/error/
mod.rs

1//! # modo::error
2//!
3//! HTTP-aware error type for the modo web framework.
4//!
5//! This module provides [`Error`], an opinionated error type that carries an HTTP status code,
6//! a human-readable message, an optional structured details payload, an optional source error, and
7//! an optional machine-readable error code. `Error` implements [`axum::response::IntoResponse`],
8//! so it can be returned directly from axum handlers.
9//!
10//! Provides:
11//! - [`Error`] — primary framework error with status code, message, optional source/details/code
12//! - [`Result<T>`] — type alias for `std::result::Result<T, Error>`
13//! - [`HttpError`] — lightweight `Copy` enum of common HTTP error statuses, converts into `Error`
14//!
15//! Automatic `From` conversions are provided for [`std::io::Error`], [`serde_json::Error`],
16//! and [`serde_yaml_ng::Error`].
17//!
18//! # Quick start
19//!
20//! ```rust
21//! use modo::error::{Error, Result};
22//!
23//! fn find_user(id: u64) -> Result<String> {
24//!     if id == 0 {
25//!         return Err(Error::not_found("user not found"));
26//!     }
27//!     Ok("Alice".to_string())
28//! }
29//! ```
30//!
31//! # Error identity
32//!
33//! After `Error` is converted into an HTTP response, the source error is discarded. Use
34//! [`Error::with_code`] to attach a static code string that survives through the response
35//! pipeline and can be read back via [`Error::error_code`].
36//!
37//! ```rust
38//! use modo::error::Error;
39//!
40//! let err = Error::unauthorized("unauthorized")
41//!     .with_code("jwt:expired");
42//! assert_eq!(err.error_code(), Some("jwt:expired"));
43//! ```
44
45mod convert;
46mod core;
47mod http_error;
48
49pub use core::{Error, Result};
50pub use http_error::HttpError;