Error Forge is a Rust error-handling crate built around a few simple ideas:
- Errors should carry stable metadata such as kind, retryability, status code, and exit code.
- Application code should be able to add context without destroying the original cause chain.
- Operational tooling should have clear hooks for logging, formatting, codes, and recovery policies.
- Feature-gated integrations should stay optional so the core remains lightweight.
It ships with a built-in AppError, a declarative define_errors! macro, an optional #[derive(ModError)] proc macro, error collectors, registry support, console formatting, synchronous retry and circuit-breaker primitives, and async-specific traits behind feature flags.
Installation
[]
= "1.0.2"
MSRV: Rust 1.81. CI builds and tests the crate on the exact 1.81.0 toolchain.
Common optional features:
derive: enables#[derive(ModError)]async: enablesAsyncForgeErrorserde: enables serialization support where compatiblelog: enables thelogadaptertracing: enables thetracingadapterjitter: enables ±20% jitter inExponentialBackoff(pulls inrand)
The console, backtrace, registry, collector and context features exist but currently gate nothing: console formatting, the error-code registry, collectors and context wrapping are always compiled in, and no backtrace is captured automatically. They are kept so existing Cargo.toml files keep resolving; enabling them has no effect.
Quick Start
Built-in AppError
use ;
Defining Custom Errors With define_errors!
define_errors! is the lowest-friction way to create a custom error enum with generated constructors and ForgeError metadata.
use ;
use io;
define_errors!
Notes:
#[kind(...)]is required for each variant;#[error(display = ...)]is optional and may come before or after it. Doc comments and other attributes on variants are kept, and the trailing comma is optional.- The recognised
#[kind]tags arecaption,retryable,fatal,statusandexit. Any other tag is a compile error. - The display string is a
format!string: list fields after it ("{path}", path) or name them inline ("{path:?}"). Only the fields it formats needDisplay/Debug. Variants without a display string use"<caption>: <Variant> | field = value", which needsDebugon every field (andDisplayon asourcefield). - Constructors are generated from the lowercase variant name, such as
ServiceError::config(...). - A field named
sourceparticipates instd::error::Error::source()chaining. - For custom
sourcefield types, implementerror_forge::macros::ErrorSourcein your crate. - With the
serdefeature enabled, source fields must themselves be serializable if you want to derive serialization through the macro-generated enum. - The metadata methods (
kind,caption,is_retryable,is_fatal,status_code,exit_code) are generated as inherent methods; the macro does not implement theForgeErrortrait. To pass the enum togroup!,ForgeErrorRecovery,log_errororprint_error, add a shortimpl ForgeErrorthat delegates to those methods (thegroup!API docs show one).
Adding Context Without Losing the Original Error
use ;
Collecting Multiple Errors
use ;
Derive Macro
Enable the derive feature to use #[derive(ModError)].
use ;
Supported derive attributes:
error_prefixerror_displayerror_kinderror_captionerror_retryableerror_http_statuserror_exit_codeerror_fatal
Every attribute accepts both the list form (#[error_http_status(404)]) and the name-value form (#[error_http_status = 404]). A bare #[error_retryable] or #[error_fatal] means true, and an explicit (false) / = false is honoured. A value of the wrong type or out of range (#[error_http_status("404")]) is a compile error.
On a struct, only error_prefix is read today: the struct displays as "<prefix>: Error" and uses the ForgeError defaults for the other metadata. The other attributes are ignored when placed on a struct.
Recovery and Resilience
The recovery module is intentionally synchronous today. It is designed for blocking code, worker threads, and service wrappers where a small sleep is acceptable.
use ;
If you need async retries, keep Error Forge for modeling and classification, then wrap retry behavior with your async runtime of choice.
Hooks, Logging, and Formatting
Error Hooks
use ;
The hook runs on the thread that created the error. Errors created inside the hook itself (for example by a failing log sink) do not call it again, and a panic inside the hook is caught so it does not unwind through the error constructor.
Logging Adapters
logging::register_logger(...)installs a custom logger once.logging::log_impl::init()is available with thelogfeature.logging::tracing_impl::init()is available with thetracingfeature.
Console Output
use ;
Error Codes
Attach stable codes to errors when you want machine-readable identifiers or documentation links.
use ;
Known Limitations
Ambiguous method calls with the async feature
AppError implements both ForgeError and, with the async feature, AsyncForgeError. The two traits have methods with the same names (kind, caption, is_retryable, ...). Cargo features are unified across the dependency graph, so if any crate in your build enables async, a call such as error.kind() fails with E0034 ("multiple applicable items in scope") wherever both traits are imported, for example through use error_forge::*.
Import only the trait you call, or use qualified syntax:
use ;
ResultExt::context and anyhow::Context
error_forge::ResultExt and anyhow::Context both add a context method to Result. With both traits in scope, result.context(...) is ambiguous. Import only one of them in a module, or call the error-forge method by path:
use ;
Quality Bar
Every push runs the following on a Linux + macOS + Windows matrix
across nine feature combinations, plus dedicated MSRV (1.81.0),
minimal-versions and cargo audit jobs:
cargo build --workspace --all-featurescargo test --workspaceper feature combination, including the README examples anddocs/API.mdas doctests and compile-pass / compile-fail tests for the macroscargo clippy --workspace --all-targets --all-features -- -D warningscargo clippy --workspace --all-targets -- -D warningscargo doc --workspace --all-features --no-depsunderRUSTDOCFLAGS="-D warnings"cargo +1.81 buildandcargo +1.81 test(MSRV)- a build on
1.81with every direct dependency at its declared minimum version cargo auditagainst the RustSec advisory database
Documentation
- API reference — narrative walkthroughs.
- API-FREEZE-AUDIT — canonical manifest of every public symbol in
1.0.0. - STABILITY — binding SemVer / panic-safety / MSRV / deprecation policy.
- COMPARISON — side-by-side with
anyhow,thiserror,miette,snafu,eyre. - Architecture — design rationale.
- Migration — upgrade guides (
0.9.x → 1.0.0, etc.). - Examples — runnable code.
- docs.rs — generated docs.
License
Licensed under Apache-2.0.