tera icon indicating copy to clipboard operation
tera copied to clipboard

missing details when render failed

Open XyLyXyRR opened this issue 4 years ago • 7 comments

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".

XyLyXyRR avatar Nov 14 '20 14:11 XyLyXyRR

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 avatar Nov 15 '20 08:11 Keats

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",
}

XyLyXyRR avatar Nov 23 '21 13:11 XyLyXyRR

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?

JMLX42 avatar May 04 '22 07:05 JMLX42

Which line? Your link is pointing to a file.

Keats avatar May 04 '22 08:05 Keats

@Keats my bad, here we go again:

https://github.com/Keats/tera/blob/4f6b354185e1f041aef5a4a6e42451d5cc348668/src/errors.rs#L69

JMLX42 avatar May 04 '22 09:05 JMLX42

I don't think so, the issue is that we are not displaying the source as well

Keats avatar May 04 '22 20:05 Keats

Any reason not to make the source just public?

koen-serry avatar Jul 04 '23 05:07 koen-serry