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//!
12//! - [`Error`] — primary framework error with status code, message, optional source/details/code
13//! - [`Result`] — type alias for `std::result::Result<T, Error>`
14//! - [`HttpError`] — lightweight `Copy` enum of common HTTP error statuses, converts into [`Error`]
15//!
16//! Automatic [`From`] conversions into [`Error`] are provided for [`std::io::Error`]
17//! (→ 500), [`serde_json::Error`] (→ 400), and [`serde_yaml_ng::Error`] (→ 500).
18//!
19//! ## Quick start
20//!
21//! ```rust
22//! use modo::error::{Error, Result};
23//!
24//! fn find_user(id: u64) -> Result<String> {
25//!     if id == 0 {
26//!         return Err(Error::not_found("user not found"));
27//!     }
28//!     Ok("Alice".to_string())
29//! }
30//! ```
31//!
32//! ## Error identity
33//!
34//! After [`Error`] is converted into an HTTP response, the source error is discarded. Use
35//! [`Error::with_code`] to attach a static code string that survives through the response
36//! pipeline and can be read back via [`Error::error_code`].
37//!
38//! ```rust
39//! use modo::error::Error;
40//!
41//! let err = Error::unauthorized("unauthorized")
42//!     .with_code("jwt:expired");
43//! assert_eq!(err.error_code(), Some("jwt:expired"));
44//! ```
45
46mod convert;
47mod core;
48mod http_error;
49
50pub use core::{Error, Result};
51pub use http_error::HttpError;