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
[]
= "0.9.7"
Common optional features:
derive: enables#[derive(ModError)]async: enablesAsyncForgeErrorserde: enables serialization support where compatiblelog: enables thelogadaptertracing: enables thetracingadapter
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.- 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.
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
Both list-style and name-value forms are supported for error_prefix.
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 ;
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 ;
Quality Bar
The crate is validated with:
cargo test --all-featurescargo clippy --all-targets --all-features -- -D warnings- targeted examples and feature-gated regression coverage
Documentation
- API reference:
docs/API.md - Examples:
examples/ - Crate documentation: https://docs.rs/error-forge
License
Licensed under Apache-2.0.