5 releases

0.2.2 Oct 21, 2024
0.2.1 Aug 20, 2024
0.2.0 Aug 24, 2023
0.1.1 Jul 6, 2022
0.1.0 Jan 28, 2021

#170 in Rust patterns

Download history 19876/week @ 2024-07-26 19553/week @ 2024-08-02 19898/week @ 2024-08-09 24665/week @ 2024-08-16 27087/week @ 2024-08-23 23274/week @ 2024-08-30 22993/week @ 2024-09-06 22107/week @ 2024-09-13 26941/week @ 2024-09-20 26923/week @ 2024-09-27 27695/week @ 2024-10-04 29596/week @ 2024-10-11 25169/week @ 2024-10-18 30028/week @ 2024-10-25 29103/week @ 2024-11-01 31201/week @ 2024-11-08

119,730 downloads per month
Used in 21 crates (4 directly)

Apache-2.0/MIT

12KB
142 lines

display-error-chain

A lightweight library for displaying errors and their sources.

A sample output:

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"
);

License: Apache-2.0/MIT

No runtime deps