error-rail
Composable, metadata-friendly error utilities for Rust.
error-rail provides a middle ground between simple string errors and full-blown tracing systems. It allows you to attach rich, structured context to errors and collect multiple validation failures without early returns.
Quick Start
New to error-rail? Check out the Quick Start Guide for a beginner-friendly introduction.
Key Features
1. Structured Context
Wrap any error in ComposableError and attach layered metadata like messages, file locations, and tags.
use ;
let err = new
.with_context
.with_context
.with_context;
// Print the full error chain
println!;
// Output shows structured context with location, tag, and message
2. Validation Accumulation
Collect multiple errors instead of failing fast. Ideal for form validation or batch processing.
use Validation;
let v1 = valid;
let v2 = invalid;
let combined: = vec!.into_iter.collect;
assert!;
// You can iterate over the accumulated errors
if let Invalid = combined
3. Performance Optimization
Use lazy context evaluation to avoid expensive string formatting on the success path.
use ;
let result = new
.with_context // Only runs if error occurs
.finish_boxed;
Note: The
context!macro usesLazyContextinternally. This means theformat!call and its arguments are evaluated only if an error actually occurs.In synthetic microbenchmarks, attaching a lazy context on the success path measured within a few percent of a baseline pipeline. Eagerly formatting the same payload can be orders of magnitude slower on the success path. On the error path, lazy and eager contexts have similar cost because both must format the message.
4. Error Pipeline
Chain context and transformations in a fluent interface.
use ;
let result = new
.with_context
.with_context
.map_err
.with_context
.finish_boxed;
5. Ergonomic Traits
IntoErrorContext: Convert your own types into error context.WithError: Transform error types while preserving success values.ErrorOps: Unified operations for recovery and mapping.
Module Overview
-
context: Functions for wrapping errors with context:with_context/with_context_result- Add context to errorsaccumulate_context- Attach multiple contexts at onceerror_pipeline- Create error processing pipelinesformat_error_chain- Format errors as human-readable chains
-
convert: Convert betweenResult,Validation, andComposableError. -
macros: Ergonomic shortcuts for context creation:context!- Lazy string formatting (deferred until error occurs)location!- Capture source file/line automaticallytag!- Add categorical tagsmetadata!- Attach key-value pairsrail!- Shorthand forErrorPipeline::new(...).finish_boxed()
-
traits: Core traits for error handling:IntoErrorContext- Convert types to error contextErrorOps- Recovery and mapping operationsWithError- Remap error typesErrorCategory- Categorical abstraction (internal)
-
types: Error structures and utilities:ComposableError<E, C>- Main error wrapper with context stackErrorContext- Structured metadata (messages, locations, tags, metadata)ErrorPipeline<T, E>- Builder for chaining context and transformationsLazyContext<F>- Deferred context evaluation for performance
-
validation: Accumulating validation results:Validation<E, A>- Either valid value or accumulated errors- Iterators and collectors for aggregating multiple validations
Installation
Examples
Run the bundled examples to see error-rail in action:
License
Apache-2.0