From bcfe6ff9ce37fe7a15906cb215a18a2ff06b2f7a Mon Sep 17 00:00:00 2001 From: Chip Senkbeil Date: Fri, 14 Jul 2023 14:40:06 -0500 Subject: [PATCH] Make JSON error capture include verbose error output as well --- src/lib.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ce28d58..57adf26 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -122,18 +122,27 @@ impl Termination for MainResult { CliError::Exit(code) => ExitCode::from(code), CliError::Error(x) => { match self.format { + // For anyhow, we want to print with debug information, which includes the + // full stack of information that anyhow collects; otherwise, we would only + // include the top-level context. Format::Shell => eprintln!("{x:?}"), + Format::Json => println!( "{}", serde_json::to_string(&serde_json::json!({ "type": "error", - "msg": x.to_string(), + "msg": format!("{x:?}"), }),) .expect("Failed to format error to JSON") ), } + + // For anyhow, we want to log with debug information, which includes the full + // stack of information that anyhow collects; otherwise, we would only include + // the top-level context. ::log::error!("{x:?}"); ::log::logger().flush(); + ExitCode::FAILURE } },