Crates.io | error2 |
lib.rs | error2 |
version | |
source | src |
created_at | 2024-12-11 07:05:35.25046 |
updated_at | 2024-12-11 07:11:21.705813 |
description | A simple error handle library for Rust |
homepage | https://github.com/ZihanType/error2 |
repository | https://github.com/ZihanType/error2 |
max_upload_size | |
id | 1479647 |
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` |
size | 0 |
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.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());
}
}