4 个版本
新 0.2.1 | 2024 年 8 月 20 日 |
---|---|
0.2.0 | 2023 年 8 月 24 日 |
0.1.1 | 2022 年 7 月 6 日 |
0.1.0 | 2021 年 1 月 28 日 |
#329 in Rust 模式
86,205 每月下载量
在 19 个crate中使用 (直接使用 4 个)
12KB
141 行
display-error-chain
一个轻量级的用于显示错误及其来源的库。
示例输出
macro_rules! impl_error {
// ...
}
// `TopLevel` is caused by a `MidLevel`.
#[derive(Debug)]
struct TopLevel;
impl_error!(TopLevel, "top level", Some(&MidLevel));
// `MidLevel` is caused by a `LowLevel`.
#[derive(Debug)]
struct MidLevel;
impl_error!(MidLevel, "mid level", Some(&LowLevel));
// `LowLevel` is the cause itself.
#[derive(Debug)]
struct LowLevel;
impl_error!(LowLevel, "low level", None);
// Now let's see how it works:
let formatted = display_error_chain::DisplayErrorChain::new(&TopLevel).to_string();
assert_eq!(
formatted,
"\
top level
Caused by:
-> mid level
-> low level"
);
// Or with `.chain()` helper:
use display_error_chain::ErrorChainExt as _;
let formatted = TopLevel.chain().to_string();
assert_eq!(
formatted,
"\
top level
Caused by:
-> mid level
-> low level"
);
// Or even with `.into_chain()` helper to consume the error.
use display_error_chain::ErrorChainExt as _;
let formatted = TopLevel.into_chain().to_string();
assert_eq!(
formatted,
"\
top level
Caused by:
-> mid level
-> low level"
);
许可证:Apache-2.0/MIT