From bd7b80dede5f1e270405588e25d1da9ea62892d7 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Thu, 11 Jan 2024 17:22:49 -0600 Subject: [PATCH] main: Improve error messages from Tera The `to_string()` method of `tera::Error` structures only returns the string value for itself; the messages from the underlying errors are not included. This effectively means any error from Tera just becomes "failed to render template." To provide a complete message, we have to recursively call `source()` on the error chain and append its string representation to the error message. --- src/main.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 21fc69c..9ae6187 100644 --- a/src/main.rs +++ b/src/main.rs @@ -132,7 +132,8 @@ fn process_instructions( let out = match tera.render(&i.template, &ctx) { Ok(o) => o, Err(e) => { - error!("Failed to render template {}: {}", i.template, e); + let msg = format_error(&e); + error!("Failed to render template {}: {}", i.template, msg); continue; } }; @@ -229,3 +230,13 @@ fn chmod( info!("Set mode of {} to {:o}", path.as_ref().display(), newmode); Ok(()) } + +fn format_error(e: &dyn std::error::Error) -> String { + // TODO replace this with std::error::Error::sources when it is stablized. + // https://github.com/rust-lang/rust/issues/58520 + let mut msg = e.to_string(); + if let Some(e) = e.source() { + msg.push_str(&format!(": {}", format_error(e))); + } + msg +}