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    // Use ": " as separator to match anyhow's `format!("{:#}", err)` output
15    // See: https://github.com/rerun-io/rerun/issues/8681
16    let mut string = error.to_string();
17    for source in std::iter::successors(error.source(), |error| error.source()) {
18        string.push_str(": ");
19        string.push_str(&source.to_string());
20    }
21    string
22}
23
24#[test]
25fn test_format() {
26    let err = anyhow::format_err!("root_cause")
27        .context("inner_context")
28        .context("outer_context");
29
30    assert_eq!(err.to_string(), "outer_context"); // Oh no, we don't see the root cause!
31
32    // Now we do:
33    assert_eq!(format(&err), "outer_context: inner_context: root_cause");
34}