resext
Main crate providing error handling with context chains
This is the primary interface for ResExt. It re-exports either the proc macro or declarative macro based on feature flags.
Installation
[]
= "0.8"
Quick Example
use resext;
Proc Macro (Default)
The proc macro provides clean syntax with full customization:
Attribute Options
prefix- String prepended to entire error messagesuffix- String appended to entire error messagemsg_prefix- String prepended to each context messagemsg_suffix- String appended to each context messagemsg_delimiter- Separator between context messages | default: " - " (NOTE: the delimiter always includes a newline before it, e.g. if delimiter = " - ", then messages will have "\n - " between them, not just " - ")source_prefix- String prepended to source error (default: "Error: ")include_variant- Include variant name in Display output (default: false)alias- Custom type alias name (default:Res)
Generated Items
The macro generates:
DisplayandErrorimplementations for your enumResErrstruct wrapping your enum with contextResExttrait with context methodsFromimplementations for automatic conversion- Type alias for
Result<T, ResErr>
Declarative Macro (Zero Dependencies)
For projects that need minimal dependencies:
[]
= { = "0.8", = false, = ["declarative"] }
use ResExt;
ResExt!
Limitations of Declarative Macro
- No custom formatting options
- Less ergonomic syntax
- No named field support
- Limited customization
Advantages of Declarative Macro
- Zero dependencies (no syn, quote, proc-macro2)
- Faster compile times
- Simpler implementation
Context Methods
.context(msg)
Add static context to an error:
read
.context?;
.with_context(|| msg)
Add dynamic context (computed only on error):
read
.with_context?;
.or_exit(code)
Exit process with given code on error:
let config = load_config.or_exit;
.better_expect(|| msg, code)
Like or_exit but with custom message:
let data = load_critical_data
.better_expect;
Error Display Format
Errors are displayed with context chains:
Failed to load application
- Failed to read config file
- Failed to open file
Error: No such file or directory
With include_variant = true:
Failed to load application
- Failed to read config file
Error: Io: No such file or directory
Comparison with Alternatives
vs anyhow
- ResExt: Type-safe, explicit error enums
- anyhow: Type-erased, dynamic errors
Use ResExt for libraries (explicit error types), anyhow for applications (flexible error handling).
vs thiserror
- ResExt: Context chains built-in
- thiserror: Manual error wrapping
Use ResExt when you need context propagation, thiserror for simple error types.
Examples
Basic Error Handling
Multiple Error Types
async
Named Fields
Migration from v0.7.0
Old declarative syntax:
ResExt!
New proc macro syntax:
To keep using the declarative macro:
[]
= { = "0.8", = false, = ["declarative"] }
Contributing
See CONTRIBUTING.md for guidelines.
License
MIT - See LICENSE for details.