re_error/
lib.rs

1//! Helpers for error handling.
2
3/// Format an error, including its chain of sources.
4///
5/// Always use this when displaying an error, especially `anyhow::Error`.
6pub fn format(error: impl AsRef<dyn std::error::Error>) -> String {
7    format_ref(error.as_ref())
8}
9
10/// Format an error, including its chain of sources.
11///
12/// Always use this when displaying an error, especially `anyhow::Error`.
13pub fn format_ref(error: &dyn std::error::Error) -> String {
14    let mut string = error.to_string();
15    for source in std::iter::successors(error.source(), |error| error.source()) {
16        string.push_str(" -> ");
17        string.push_str(&source.to_string());
18    }
19    string
20}
21
22#[test]
23fn test_format() {
24    let err = anyhow::format_err!("root_cause")
25        .context("inner_context")
26        .context("outer_context");
27
28    assert_eq!(err.to_string(), "outer_context"); // Oh no, we don't see the root cause!
29
30    // Now we do:
31    assert_eq!(format(&err), "outer_context -> inner_context -> root_cause");
32}