-
-
Notifications
You must be signed in to change notification settings - Fork 144
Description
Description
I'm reading a variable that's passed in from a content source file. It comes in as text. I convert it to a number with int. The problem is that if the content contains a letter instead of a number the page doesn't render.
Reproduction steps
Here's an example of what I'm seeing:
---
[dependencies]
minijinja = { version = "2.9.0" }
---
use minijinja::{Environment, Value, context};
fn main() {
let test_one = Value::from("1");
let test_two = Value::from(2);
let test_three = Value::from("a");
do_output(&test_one);
do_output(&test_two);
do_output(&test_three);
}
fn do_output(input: &Value) {
let mut env = Environment::new();
env.add_template(
"example", "{{ input|int }}"
).unwrap();
if let Ok(template) = env.get_template("example") {
match template.render(context!(input)) {
Ok(output) => println!("{}", output),
Err(e) => println!("{}", e)
}
}
}The output is:
1
2
invalid operation: invalid float literal (in example:1)
The input is user generated. I don't have a way to guarantee it's a number.
Additional helpful information:
- Version of minijinja: 2.9.0
- Version of rustc: rustc 1.88.0-nightly (17ffbc81a 2025-04-04)
- Operating system and version: macOS 15.4
What did you expect
My first guess is that the result would be none. Whatever the actual case, I expected the page to render at least something instead of throwing an error at the .render() level that prevents output.
The docs list the return type as Result<Value, Error>. That makes sense, but I'd like to be able to ensure the page renders if an Error occurs (I can handle the presentation issues in the template).
Is there a way to allow that to happen?