Skip to content

Commit d05b61b

Browse files
authored
Fix unknown_method_callback error reporting (#743)
1 parent 8a94de3 commit d05b61b

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ All notable changes to MiniJinja are documented here.
77
- Do not panic if too large templates (too many lines or too many
88
columns) are loaded. The error reporting will be wrong in those
99
cases but the templates will load. #742
10+
- Fixed a bug that caused unknown method callbacks to not get
11+
proper error reporting if they cannot find a method. #743
1012

1113
## 2.8.0
1214

minijinja/src/value/mod.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,8 +1590,21 @@ impl Value {
15901590
Err(mut err) => {
15911591
if err.kind() == ErrorKind::UnknownMethod {
15921592
if let Some(ref callback) = state.env().unknown_method_callback {
1593-
return callback(state, self, name, args);
1594-
} else if err.detail().is_none() {
1593+
match callback(state, self, name, args) {
1594+
Ok(result) => return Ok(result),
1595+
Err(callback_err) => {
1596+
// if the callback fails with the same error, we
1597+
// want to also attach the default detail if
1598+
// it's missing
1599+
if callback_err.kind() == ErrorKind::UnknownMethod {
1600+
err = callback_err;
1601+
} else {
1602+
return Err(err);
1603+
}
1604+
}
1605+
}
1606+
}
1607+
if err.detail().is_none() {
15951608
err.set_detail(format!("{} has no method named {}", self.kind(), name));
15961609
}
15971610
}

minijinja/tests/test_environment.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ fn test_unknown_method_callback() {
173173

174174
let rv = env.render_str("{{ {'x': 42}.items() }}", ()).unwrap();
175175
assert_snapshot!(rv, @r###"[["x", 42]]"###);
176+
177+
let err = env.render_str("{{ [].does_not_exist() }}", ()).unwrap_err();
178+
assert_eq!(err.kind(), ErrorKind::UnknownMethod);
179+
assert_eq!(
180+
err.detail(),
181+
Some("sequence has no method named does_not_exist")
182+
);
176183
}
177184

178185
#[test]

0 commit comments

Comments
 (0)