3 unstable releases

0.2.0 Aug 24, 2023
0.1.1 Jul 6, 2022
0.1.0 Jan 28, 2021

#261 in Rust patterns

Download history 7311/week @ 2023-12-13 5676/week @ 2023-12-20 4151/week @ 2023-12-27 8902/week @ 2024-01-03 9680/week @ 2024-01-10 14630/week @ 2024-01-17 13446/week @ 2024-01-24 19278/week @ 2024-01-31 20076/week @ 2024-02-07 20994/week @ 2024-02-14 18594/week @ 2024-02-21 20817/week @ 2024-02-28 22928/week @ 2024-03-06 22087/week @ 2024-03-13 19460/week @ 2024-03-20 13660/week @ 2024-03-27

81,898 downloads per month
Used in 13 crates (4 directly)

Apache-2.0/MIT

12KB
141 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