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
parent
dba9037230
commit
bd7b80dede
13
src/main.rs
13
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue