| 1 | # display-error-chain |
| 2 | |
| 3 | <!-- cargo-rdme start --> |
| 4 | |
| 5 | A lightweight library for displaying errors and their sources. |
| 6 | |
| 7 | A sample output: |
| 8 | |
| 9 | ```rust |
| 10 | macro_rules! impl_error { |
| 11 | // ... |
| 12 | } |
| 13 | |
| 14 | // `TopLevel` is caused by a `MidLevel`. |
| 15 | #[derive(Debug)] |
| 16 | struct TopLevel; |
| 17 | impl_error!(TopLevel, "top level", Some(&MidLevel)); |
| 18 | |
| 19 | // `MidLevel` is caused by a `LowLevel`. |
| 20 | #[derive(Debug)] |
| 21 | struct MidLevel; |
| 22 | impl_error!(MidLevel, "mid level", Some(&LowLevel)); |
| 23 | |
| 24 | // `LowLevel` is the cause itself. |
| 25 | #[derive(Debug)] |
| 26 | struct LowLevel; |
| 27 | impl_error!(LowLevel, "low level", None); |
| 28 | |
| 29 | // Now let's see how it works: |
| 30 | let formatted = display_error_chain::DisplayErrorChain::new(&TopLevel).to_string(); |
| 31 | assert_eq!( |
| 32 | formatted, |
| 33 | "\ |
| 34 | top level |
| 35 | Caused by: |
| 36 | -> mid level |
| 37 | -> low level" |
| 38 | ); |
| 39 | |
| 40 | // Or with `.chain()` helper: |
| 41 | use display_error_chain::ErrorChainExt as _; |
| 42 | let formatted = TopLevel.chain().to_string(); |
| 43 | assert_eq!( |
| 44 | formatted, |
| 45 | "\ |
| 46 | top level |
| 47 | Caused by: |
| 48 | -> mid level |
| 49 | -> low level" |
| 50 | ); |
| 51 | |
| 52 | // Or even with `.into_chain()` helper to consume the error. |
| 53 | use display_error_chain::ErrorChainExt as _; |
| 54 | let formatted = TopLevel.into_chain().to_string(); |
| 55 | assert_eq!( |
| 56 | formatted, |
| 57 | "\ |
| 58 | top level |
| 59 | Caused by: |
| 60 | -> mid level |
| 61 | -> low level" |
| 62 | ); |
| 63 | ``` |
| 64 | |
| 65 | <!-- cargo-rdme end --> |
| 66 | |
| 67 | License: Apache-2.0/MIT |