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();
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"); assert_eq!(format(&err), "outer_context: inner_context: root_cause");
34}