error-rail
Composable, metadata-friendly error utilities for Rust applications that need richer
context than anyhow-style strings but lighter ceremony than full-blown tracing.
Notice
This library is an improved version that separates and refines the error module and Validated module from my Rustica library, which I created to study category theory and functional programming.
Features
- Structured context – attach messages, file/line locations, tags, and key/value metadata to any error.
- Composable collectors – aggregate successes and failures through
Validationand convert seamlessly betweenResult,Validation, andComposableError. - Ergonomic helpers – macros (
context!,location!,tag!,metadata!) and traits (IntoErrorContext,WithError) keep boilerplate low.
Installation
No default features are required; the crate depends only on serde and smallvec.
Quick start
use ;
Structured context
ComposableError<E> wraps the original error E, a stack of ErrorContext values, and
an optional error code C (default u32). Context helpers:
| Helper | Purpose |
|---|---|
context!("format {}", arg) |
Lazily format human-readable text |
location!() |
Capture file!() + line!() |
tag!("auth") |
Mark errors for later filtering |
metadata!("user_id", user_id.to_string()) |
Store arbitrary key/value pairs |
Use ErrorPipeline to chain operations and accumulate contexts
before finalizing into a ComposableError.
Validation & conversion
Validation<E, A> collects every error instead of failing fast. Key APIs include:
Validation::invalid_many,iter_errors, and iterator adapters (Iter,ErrorsIter).FromIteratorimpls for collecting iterators ofResultorValidation.
Conversions live in convert:
validation_to_result,result_to_validationwrap_in_composable_result/_boxedflatten_composable_result
Macros & traits
context!,location!,tag!,metadata!IntoErrorContextfor plugging custom types into the context stack.WithErrorfor transforming error types while preserving success values.
Examples & doctests
Run the bundled examples directly from the crate root:
This exercises the error pipeline and validation collectors end-to-end. To ensure documentation stays accurate, execute all doctests whenever you touch public APIs:
Consider wiring these commands into CI so regressions in docs or examples are caught automatically.
License
Apache-2.0