3 unstable releases
new 0.2.0 | Dec 13, 2024 |
---|---|
0.1.1 | Dec 11, 2024 |
0.1.0 | Dec 11, 2024 |
#1063 in Rust patterns
370 downloads per month
9KB
185 lines
Error2
ErrorExt
is a trait that extends the std::error::Error
trait with additional methods.
It defines two methods:
fn entry(&self) -> (Location, NextError<'_>)
, a required method, the implementer needs to return the location of the current error and the next error.fn error_stack(&self) -> Box<[Box<str>]>
, a provided method, will return the stack information of the current error.
Example
use error2::{ErrorExt, Location, NextError};
use snafu::{ResultExt, Snafu};
#[derive(Debug, Snafu)]
#[snafu(display("IO error"))]
pub struct IoError {
#[snafu(implicit)]
location: Location,
source: std::io::Error,
}
impl ErrorExt for IoError {
fn entry(&self) -> (Location, NextError<'_>) {
(self.location, NextError::Std(&self.source))
}
}
fn main() {
let result = std::fs::read("aaaaa.txt").context(IoSnafu);
if let Err(e) = result {
// Print the error stack
// [
// "0: IO error, at src/main.rs:19:45",
// "1: No such file or directory (os error 2)",
// ]
println!("{:#?}", e.error_stack());
}
}
Dependencies
~135KB