Anyhow-Tracing
An extension of the anyhow crate that provides named fields on an equivalent
of anyhow::Error. Named fields are stored as a Vec<(&'static str, String)>
to allow for passing the error object around as an owned instance.
Features
- Named Fields: Add structured key-value fields to errors for better debugging and logging.
- Drop-in Replacement: Compatible macros that work like
anyhow!,bail!, andensure!. - Error Chaining: Preserves error chains while maintaining named fields, new named fields are added to the error.
- Debug and Display Fields: Support for both
DisplayandDebugformatting of field values. - Context Extension: Extends the
Contexttrait to work with named fields.
Quick Start
Add this to your Cargo.toml:
[]
= "0.1"
Basic Usage
use ;
// Create errors with named fields
let err: Error = anyhow!;
// Access field values
println!;
println!;
// Error with debug formatting
let data = vec!;
let err: Error = anyhow!;
// Using bail! macro
// Using ensure! macro
// Error chaining with context
let result: = Err;
let final_result = match result ;
// Working with standard library errors
use fs;
let result = read_to_string
.with_field
.with_field;
Context Trait Extensions
The crate extends the Context trait to work with standard library types:
use ;
use File;
// On Result types
let result: = open
.with_field;
// On Option types
let maybe_value: = None;
let result: = maybe_value
.with_field
.context;
Compatibility
This crate is designed to be a drop-in replacement for anyhow with additional functionality. Most anyhow code should work with minimal changes, primarily requiring:
- Import changes:
anyhow→anyhow_tracing - Macro syntax: Use
,to separate fields and messages - similar totracing::event!. - Method chaining: Be explicit about context operations to avoid trait conflicts
📄 License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.