4 个版本 (2 个破坏性更改)

0.3.0 2023 年 9 月 5 日
0.2.0 2023 年 3 月 22 日
0.1.1 2022 年 12 月 23 日
0.1.0 2022 年 12 月 21 日

#1353 in Rust 模式

Download history · Rust 包仓库 60/week @ 2024-03-11 · Rust 包仓库 59/week @ 2024-03-18 · Rust 包仓库 31/week @ 2024-03-25 · Rust 包仓库 58/week @ 2024-04-01 · Rust 包仓库 72/week @ 2024-04-08 · Rust 包仓库 23/week @ 2024-04-15 · Rust 包仓库 100/week @ 2024-04-22 · Rust 包仓库 99/week @ 2024-04-29 · Rust 包仓库 127/week @ 2024-05-06 · Rust 包仓库 123/week @ 2024-05-13 · Rust 包仓库 97/week @ 2024-05-20 · Rust 包仓库 54/week @ 2024-05-27 · Rust 包仓库 21/week @ 2024-06-03 · Rust 包仓库 25/week @ 2024-06-10 · Rust 包仓库 19/week @ 2024-06-17 · Rust 包仓库 19/week @ 2024-06-24 · Rust 包仓库

91 每月下载
用于 zipsign

MIT/Apache

18KB
258

pretty-error-debug

GitHub Workflow Status Crates.io Minimum supported Rust version: 1.56 License: MIT OR Apache-2.0

显示错误链。在您的 fn main() 中,作为 Result<(), E> 使用,并配合 thiserror 使用。

此软件包简单地 剽窃 提取 了来自 anyhow 的所有相关格式化代码。

示例消息

Error: Got a 'middle' error

Caused by:
    1: A nested error occured
    2: 'inner' failed
    3: Caught an error: Not implemented, yet.

使用 thiserror

#[derive(pretty_error_debug::Debug, thiserror::Error)]
pub enum MyError {
    #[error("Error variant 1 happened")]
    Variant1(#[from] Error1),
    #[error("Error variant 2 happened")]
    Variant2(#[from] Error2),
}

fn main() -> Result<(), MyError> {
    ...
}

使用 thiserror,但没有新类型

#[derive(Debug, thiserror::Error)]
pub enum MyError {
    #[error("Error variant 1 happened")]
    Variant1(#[from] Error1),
    #[error("Error variant 2 happened")]
    Variant2(#[from] Error2),
}

fn main() -> Result<(), pretty_error_debug::Wrapper<MyError>> {
    ...
}

未使用 thiserror

use std::error::Error;
use std::fmt::{self, Write};

#[derive(pretty_error_debug::Debug)]
pub enum MyError {
    Variant1(Error1),
    Variant2(Error2),
}

impl fmt::Display for MyError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MyError::Variant1(_) => write!(f, "Error variant 1 happened"),
            MyError::Variant2(_) => write!(f, "Error variant 2 happened"),
        }
    }
}

impl Error for MyError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            MyError::Variant1(source) => Some(source),
            MyError::Variant2(source) => Some(source),
        }
    }
}

fn main() -> Result<(), MyError> {
    ...
}

依赖项

~0–430KB