From 43ca74549d1a9a7930d9ded7ab24eda16cef0dcb Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Tue, 30 Apr 2024 14:54:42 -0400 Subject: [PATCH] Include the whole error message in errors from Rust (#3415) --- taskchampion/lib/src/util.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/taskchampion/lib/src/util.rs b/taskchampion/lib/src/util.rs index bfd739282..61223e6bf 100644 --- a/taskchampion/lib/src/util.rs +++ b/taskchampion/lib/src/util.rs @@ -1,7 +1,15 @@ use crate::string::RustString; -pub(crate) fn err_to_ruststring(e: impl std::string::ToString) -> RustString<'static> { - RustString::from(e.to_string()) +pub(crate) fn err_to_ruststring(e: anyhow::Error) -> RustString<'static> { + // The default `to_string` representation of `anyhow::Error` only shows the "outermost" + // context, e.g., "Could not connect to server", and omits the juicy details about what + // actually went wrong. So, join all of those contexts with `: ` for presentation to the C++ + // layer. + let entire_msg = e + .chain() + .skip(1) + .fold(e.to_string(), |a, b| format!("{}: {}", a, b)); + RustString::from(entire_msg) } /// An implementation of Vec::into_raw_parts, which is still unstable. Returns ptr, len, cap.