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.
master
Dustin 2024-01-11 17:22:49 -06:00
parent dba9037230
commit bd7b80dede
1 changed files with 12 additions and 1 deletions

View File

@ -132,7 +132,8 @@ fn process_instructions(
let out = match tera.render(&i.template, &ctx) { let out = match tera.render(&i.template, &ctx) {
Ok(o) => o, Ok(o) => o,
Err(e) => { Err(e) => {
error!("Failed to render template {}: {}", i.template, e); let msg = format_error(&e);
error!("Failed to render template {}: {}", i.template, msg);
continue; continue;
} }
}; };
@ -229,3 +230,13 @@ fn chmod(
info!("Set mode of {} to {:o}", path.as_ref().display(), newmode); info!("Set mode of {} to {:o}", path.as_ref().display(), newmode);
Ok(()) 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
}