tera
tera copied to clipboard
missing details when render failed
Hi there, ahh, I've got an error, and I'm trying print it, but it didn't print the details.
main.rs
use std::{collections::HashMap, error::Error};
use tera::{Context, Tera};
fn main() {
let t = Tera::new("tpls/**").unwrap();
let mut context = Context::new();
let mut map = HashMap::new();
map.insert("one", "key one");
map.insert("two", "key two");
context.insert("map", &map);
match t.render("index.tpl", &context) {
Ok(v) => println!("{}", v),
Err(e) => eprintln!("{}, {}", e, e.source().unwrap()),
};
}
tpls/index.tpl
{%- for k,v in map %}
{{ k }}: {{ v.name }}
{%- endfor -%}
Output is Failed to render 'index.tpl', Variable `v.name` not found in context while rendering 'index.tpl'
You can see that, eprintln!("{}, {}", e, e.source().unwrap())
, I found this way can print the details.
If just eprintln!("{}", e)
then the output is just Failed to render 'index.tpl'
.
At first, I use unwrap()
, it will print all messages, then I finished these feature, I change it to match
, and then
I was confused cause this.
"Oh, this is awkward (x".
That's due to the way the errors are done in Tera, changing that would be a breaking change but it should be improved for the next major version.
Can use {:#}
for now.
use anyhow::anyhow;
fn main() {
let e = anyhow!("some errors").context("read failed");
println!("{}", e);
println!("{:#}", e);
println!("{:#?}", e);
}
read failed
read failed: some errors
Error {
context: "read failed",
source: "some errors",
}
That's due to the way the errors are done in Tera, changing that would be a breaking change but it should be improved for the next major version.
@Keats can't we just change this line to also print the source
?
Which line? Your link is pointing to a file.
@Keats my bad, here we go again:
https://github.com/Keats/tera/blob/4f6b354185e1f041aef5a4a6e42451d5cc348668/src/errors.rs#L69
I don't think so, the issue is that we are not displaying the source as well
Any reason not to make the source just public?