A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Make `wasmtime_internal_error`'s error formatting more-compatible with `anyhow`'s (#12306)
Nick Fitzgeraldf94a40c0ee67
diff --git a/crates/error/src/context.rs b/crates/error/src/context.rsindex 5e2157334e..bbcc97f354 100644--- a/crates/error/src/context.rs+++ b/crates/error/src/context.rs@@ -73,7 +73,7 @@ mod sealed {/// failed to convert `999` into a `u8` (max = `255`)////// Caused by:-/// 0: out of range integral type conversion attempted+/// out of range integral type conversion attempted/// "#.trim(),/// );/// ```diff --git a/crates/error/src/error.rs b/crates/error/src/error.rsindex ac3bc670b5..db3eb0c00f 100644--- a/crates/error/src/error.rs+++ b/crates/error/src/error.rs@@ -446,8 +446,14 @@ impl fmt::Debug for Error {if let Some(source) = inner.source() {f.write_str("\n\nCaused by:\n")?;+ let multiple_causes = source.source().is_some();for (i, e) in Chain::new(source).enumerate() {- writeln!(f, "\t{i}: {e}")?;+ if multiple_causes {+ write!(f, "{i: >5}: ")?;+ } else {+ write!(f, " ")?;+ }+ writeln!(f, "{e}")?;}}@@ -717,8 +723,8 @@ impl Error {/// cannot frob the blobbins////// Caused by:- /// 0: failed to bonkinate- /// 1: root cause+ /// 0: failed to bonkinate+ /// 1: root cause/// "#.trim(),/// ),/// );diff --git a/crates/error/tests/tests.rs b/crates/error/tests/tests.rsindex 76edb67ea3..9fa25285a5 100644--- a/crates/error/tests/tests.rs+++ b/crates/error/tests/tests.rs@@ -640,11 +640,39 @@ fn fmt_debug() {let error = Error::msg("whoops").context("uh oh").context("yikes");let actual = format!("{error:?}");- let expected = "yikes\n\- \n\- Caused by:\n\- \t0: uh oh\n\- \t1: whoops\n";+ let expected = "\+yikes++Caused by:+ 0: uh oh+ 1: whoops+";++ #[cfg(feature = "backtrace")]+ {+ assert!(actual.starts_with(expected));+ if let BacktraceStatus::Captured = error.backtrace().status() {+ assert!(actual.contains("Stack backtrace:"));+ }+ }++ #[cfg(not(feature = "backtrace"))]+ {+ assert_eq!(actual, expected);+ }+}+#[test]+fn fmt_debug_with_single_cause() {+ let error = Error::msg("whoops").context("uh oh");+ let actual = format!("{error:?}");++ // NB: the causes are only numbered when there are multiple of them.+ let expected = "\+uh oh++Caused by:+ whoops+";#[cfg(feature = "backtrace")]{