Skip to content

Commit

Permalink
Qute: print origin if non-literal value used in bracket notation
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba authored and bschuhmann committed Nov 16, 2024
1 parent f907550 commit 2556238
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1064,9 +1064,11 @@ private static Part createPart(Supplier<Integer> idGenerator, String namespace,
value = literal.toString();
} else {
throw TemplateException.builder()
.message((literal == null ? "Null" : "Non-literal")
+ " value used in bracket notation [{value}] {origin}")
.message((literal == null ? "Null value" : "Non-literal value [{value}]")
+ " used in bracket notation in expression \\{{expr}\\}{#if origin.hasNonGeneratedTemplateId??} in{origin}{/if}")
.argument("value", value)
.argument("expr", exprValue)
.origin(origin)
.build();
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand Down Expand Up @@ -468,6 +469,15 @@ public void testSectionParameterWithNestedSingleQuotationMark() {

}

@Test
public void testNonLiteralBracketNotation() {
TemplateException e = assertThrows(TemplateException.class,
() -> Engine.builder().addDefaults().build().parse("{foo[bar]}", null, "baz"));
assertNotNull(e.getOrigin());
assertEquals("Non-literal value [bar] used in bracket notation in expression {foo[bar]} in template [baz] line 1",
e.getMessage());
}

private void assertSectionParams(Engine engine, String content, Map<String, String> expectedParams) {
Template template = engine.parse(content);
SectionNode node = template.findNodes(n -> n.isSection() && n.asSection().name.equals("let")).iterator().next()
Expand Down

0 comments on commit 2556238

Please sign in to comment.