error2

Crates.ioerror2
lib.rserror2
version
sourcesrc
created_at2024-12-11 07:05:35.25046
updated_at2024-12-11 07:11:21.705813
descriptionA simple error handle library for Rust
homepagehttps://github.com/ZihanType/error2
repositoryhttps://github.com/ZihanType/error2
max_upload_size
id1479647
Cargo.toml error:TOML parse error at line 22, column 1 | 22 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
ZihanType (ZihanType)

documentation

README

Error2

Crates.io version docs.rs docs

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());
    }
}
Commit count: 6

cargo fmt