errer - smooth & flexible error management
with ideas from failure and SNAFU.
Rundown
use errer::ErrorContext;
#[derive(Errer)]
enum Error {
#[errer(from)] //implements From<request::Error>
Request(request::Error),
//implements Display
//from specifies the base for ErrorContext, which is implemented with context
//this generates a struct, akin to SNAFU, except you can use context on structs and enums of all kinds
#[errer(from, context, display = "Error parsing section {}: {}", 1, 0)]
ParsingSection { from: parse::Error, section: usize },
#[errer(from, std, display = "IO error: {}", 0)] //std implements std::error::Error if Self: Debug + Display
IO(io::Error)
}
res!(Error);
fn main_res() -> Res<()> {
let data = request::do()?;
data.parse().context(ParsingSection { section: 0 })?;
Ok(())
}
main_res!(main_res);
A few tips
- Context is dependent on
from
to specify the base error; take a peek at the internal traits for more information.
- You can use all the attributes on structs and the outside of enums (to set defaults) as well.
- You can use
#[errer(context = name)]
to set the name of the generated context struct.
- Similarly, you can use
#[errer(from = member)]
to set the member that from
uses.
- You can use
display
on the outside of enums to set a surrounding format string.
- And remember that you can always implement
Display
yourself.