#error #report #error-context #error-handling #debugging #derive-debug

no-std error-stack

支持任意附加用户数据的上下文感知错误处理库

12个版本

0.5.0 2024年7月12日
0.4.1 2023年9月4日
0.4.0 2023年8月23日
0.3.1 2023年2月8日
0.1.1 2022年6月10日

#48 in Rust模式

Download history 13570/week @ 2024-05-04 20861/week @ 2024-05-11 23640/week @ 2024-05-18 17429/week @ 2024-05-25 18135/week @ 2024-06-01 18774/week @ 2024-06-08 18227/week @ 2024-06-15 15502/week @ 2024-06-22 14638/week @ 2024-06-29 16754/week @ 2024-07-06 19131/week @ 2024-07-13 21574/week @ 2024-07-20 14898/week @ 2024-07-27 27268/week @ 2024-08-03 23380/week @ 2024-08-10 16749/week @ 2024-08-17

85,876 每月下载量
92 个crate中使用 (82 个直接使用)

MIT/Apache

225KB
3K SLoC

crates.io libs.rs rust-version documentation license discord

开放问题 / 讨论

error-stack

error-stack 是一个支持任意附加用户数据的上下文感知错误处理库。

阅读我们的 公告文章 了解其背后的故事。

该库允许在错误传播过程中围绕错误构建一个 Report

use core::fmt;

use error_stack::{Context, Report, Result, ResultExt};

#[derive(Debug)]
struct ParseExperimentError;

impl fmt::Display for ParseExperimentError {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.write_str("invalid experiment description")
    }
}

impl Context for ParseExperimentError {}

fn parse_experiment(description: &str) -> Result<(u64, u64), ParseExperimentError> {
    let value = description
        .parse::<u64>()
        .attach_printable_lazy(|| format!("{description:?} could not be parsed as experiment"))
        .change_context(ParseExperimentError)?;

    Ok((value, 2 * value))
}

#[derive(Debug)]
struct ExperimentError;

impl fmt::Display for ExperimentError {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.write_str("experiment error: could not run experiment")
    }
}

impl Context for ExperimentError {}

fn start_experiments(
    experiment_ids: &[usize],
    experiment_descriptions: &[&str],
) -> Result<Vec<u64>, ExperimentError> {
    let experiments = experiment_ids
        .iter()
        .map(|exp_id| {
            let description = experiment_descriptions.get(*exp_id).ok_or_else(|| {
                Report::new(ExperimentError)
                    .attach_printable(format!("experiment {exp_id} has no valid description"))
            })?;

            let experiment = parse_experiment(description)
                .attach_printable(format!("experiment {exp_id} could not be parsed"))
                .change_context(ExperimentError)?;

            Ok(move || experiment.0 * experiment.1)
        })
        .collect::<Result<Vec<_>, ExperimentError>>()
        .attach_printable("unable to set up experiments")?;

    Ok(experiments.iter().map(|experiment| experiment()).collect())
}

fn main() -> Result<(), ExperimentError> {
    let experiment_ids = &[0, 2];
    let experiment_descriptions = &["10", "20", "3o"];
    start_experiments(experiment_ids, experiment_descriptions)?;

    Ok(())
}

这很可能会产生一个错误并打印

Error: experiment error: could not run experiment
├╴at examples/demo.rs:50:18
├╴unable to set up experiments
│
├─▶ invalid experiment description
│   ├╴at examples/demo.rs:20:10
│   ╰╴experiment 2 could not be parsed
│
╰─▶ invalid digit found in string
    ├╴at examples/demo.rs:19:10
    ├╴backtrace with 31 frames (1)
    ╰╴"3o" could not be parsed as experiment

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

backtrace no. 1
   0: std::backtrace_rs::backtrace::libunwind::trace
             at /rustc/f3623871cfa0763c95ebd6ceafaa6dc2e44ca68f/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1: std::backtrace_rs::backtrace::trace_unsynchronized
             at /rustc/f3623871cfa0763c95ebd6ceafaa6dc2e44ca68f/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2: std::backtrace::Backtrace::create
             at /rustc/f3623871cfa0763c95ebd6ceafaa6dc2e44ca68f/library/std/src/backtrace.rs:331:13
   3: core::ops::function::FnOnce::call_once
             at /rustc/f3623871cfa0763c95ebd6ceafaa6dc2e44ca68f/library/core/src/ops/function.rs:250:5
   4: core::bool::<impl bool>::then
             at /rustc/f3623871cfa0763c95ebd6ceafaa6dc2e44ca68f/library/core/src/bool.rs:60:24
   5: error_stack::report::Report<C>::from_frame
             at ./src/report.rs:286:25
   6: error_stack::report::Report<C>::new
             at ./src/report.rs:272:9
   7: error_stack::context::<impl core::convert::From<C< for error_stack::report::Report<C>>::from
             at ./src/context.rs:83:9
   8: <core::result::Result<T,C< as error_stack::result::ResultExt<::attach_printable_lazy
             at ./src/result.rs:158:31
   9: demo::parse_experiment
             at demo.rs:17:17
  10: demo::start_experiments::{{closure}}
             at demo.rs:48:30
   (For this example: additional frames have been removed)

用法

请参阅 文档

有关 error-stack 的更多使用示例,请查看 示例文件夹

贡献者

error-stackHASH 创建和维护。作为一个开源项目,我们欢迎外部贡献,并已发布一个 贡献指南,概述了流程。如果您有任何问题,请通过我们的 Discord服务器 与我们联系。

许可证

error-stack 在多个开源许可证下可用。请参阅 LICENSE 文件以了解您的选项。

依赖关系

~0–620KB
~11K SLoC