resext
Main crate providing error handling with context chains
This is the primary interface for ResExt. It re-exports the proc-macro as well as other helpers provided by ResExt.
Installation
[]
= "1.0.0"
Quick Example
use resext;
Proc Macro
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 messagedelimiter- 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 which is used for getting the names for other items generated by the proc-macro (default:Res)buf_size- Size for the context message byte buffer (default: 64)std- Enable std-only features like.or_exit()macroallocEnable heap-spilling if context exceedsbuf_size, this attribute requires a global allocator / importingextern crate alloc
Context Methods
.context(self, msg: &str)
Add static context to an error:
read
.context?;
Ok::
.with_context(self, args: core::fmt::Arguments<'_>)
Add dynamic context:
let path = "file.txt";
read
.with_context?;
Ok::
.or_exit(self, code: i32)
Print error to Stderr and exit process with given code on error, this is an std-only method:
let config = load_config.or_exit;
.better_expect(self, msg: FnOnce() -> impl std::fmt::Display, code: i32)
Like or_exit but with custom message, this is an std-only method:
let data = load_critical_data
.better_expect;
.write_log<W: std::io::Write>(self, writer: &mut W)
Write error to writer on error and return None or return Some(T)
Writer needs to implement std::io::Write, so it's an std-only method:
let mut wtr = new;
let opt = write_output_entry.write_log;
match opt
.fmt_log<F: core::fmt::Write>(self, writer: &mut F)
Write error to writer on error and return None or return Some(T):
let mut wtr = Stringnew;
let opt = write_output_entry.write_log;
match opt
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
Examples
Basic Error Handling
Multiple Error Types
async
Named Fields
Contributing
See CONTRIBUTING.md for guidelines.
License
MIT - See LICENSE for details.