4 releases (2 breaking)

0.3.0 Sep 5, 2023
0.2.0 Mar 22, 2023
0.1.1 Dec 23, 2022
0.1.0 Dec 21, 2022

#1224 in Rust patterns

Download history 12/week @ 2024-01-10 33/week @ 2024-01-17 15/week @ 2024-01-24 41/week @ 2024-01-31 31/week @ 2024-02-07 10/week @ 2024-02-14 57/week @ 2024-02-21 72/week @ 2024-02-28 38/week @ 2024-03-06 61/week @ 2024-03-13 57/week @ 2024-03-20 57/week @ 2024-03-27 68/week @ 2024-04-03 40/week @ 2024-04-10 71/week @ 2024-04-17 75/week @ 2024-04-24

271 downloads per month
Used in zipsign

MIT/Apache

18KB
258 lines

pretty-error-debug

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

Display a the chain of an error. Most useful as Result<(), E> for your fn main(), and in conjunction with thiserror.

This crate simply plagiarized extracted all the relevant formatting code from anyhow.

Example message

Error: Got a 'middle' error

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

With 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> {
    ...
}

With thiserror, but without a new type

#[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>> {
    ...
}

Without 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> {
    ...
}

Dependencies

~0–430KB