From add338d3b9fbfeb93e41f77072d67db8085c4acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Ma=C4=87kowski?= Date: Sun, 22 Mar 2026 16:19:56 +0100 Subject: [PATCH] feat: improve error printing When using `Result::expect()`, (e.g. in the default `cot::main`) errors are printed using `Debug` printer. This causes very ugly errors to be produced. Instead, when the alternate mode is not being used, just format the error nicely, along with the error source chain. This roughly duplicates the `anyhow::Error` error printing logic: https://docs.rs/anyhow/latest/anyhow/struct.Error.html#method.chain --- cot-core/src/error/error_impl.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cot-core/src/error/error_impl.rs b/cot-core/src/error/error_impl.rs index db594083..3808d3b3 100644 --- a/cot-core/src/error/error_impl.rs +++ b/cot-core/src/error/error_impl.rs @@ -171,7 +171,26 @@ impl Error { impl Debug for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - Debug::fmt(&self.repr, f) + // If the alternate (`{:#?}`) formatting has been specified, print out the + // `Debug` formatting normally. If not, (which is the case when using + // `Result::unwrap()` or `Result::expect()`) pretty print the error. + if f.alternate() { + Debug::fmt(&self.repr, f) + } else { + Display::fmt(&self.repr.inner, f)?; + + writeln!(f)?; + writeln!(f)?; + writeln!(f, "caused by:")?; + let mut source = self.source(); + while let Some(e) = source { + writeln!(f, " {e}")?; + + source = e.source(); + } + + Ok(()) + } } }