1pub fn format(error: impl AsRef<dyn std::error::Error>) -> String {
7 format_ref(error.as_ref())
8}
9
10pub 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"); assert_eq!(format(&err), "outer_context -> inner_context -> root_cause");
32}