#error-handling #error-chain #cli

no-std pretty-error-debug

If the process ends with an Error, write out the Error message and chain

5 unstable releases

0.3.2 May 7, 2025
0.3.1 Dec 4, 2024
0.3.0 Sep 5, 2023
0.2.0 Mar 22, 2023
0.1.1 Dec 23, 2022

#449 in Rust patterns

Download history 360/week @ 2025-06-01 370/week @ 2025-06-08 400/week @ 2025-06-15 317/week @ 2025-06-22 324/week @ 2025-06-29 342/week @ 2025-07-06 284/week @ 2025-07-13 367/week @ 2025-07-20 410/week @ 2025-07-27 1425/week @ 2025-08-03 650/week @ 2025-08-10 443/week @ 2025-08-17 301/week @ 2025-08-24 277/week @ 2025-08-31 185/week @ 2025-09-07 157/week @ 2025-09-14

946 downloads per month
Used in 7 crates (6 directly)

MIT/Apache

20KB
256 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

~98KB