Skip to content

Commit

Permalink
default error modes set per specification of flavours
Browse files Browse the repository at this point in the history
  • Loading branch information
msangel committed Aug 1, 2023
1 parent 9544a04 commit 58289f2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/liqp/TemplateContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class TemplateContext {
private Map<String, Object> environmentMap;
private Map<String, Object> registry;

private List<Exception> errors;
private final List<Exception> errors;
private final Template template;

public TemplateContext() {
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/liqp/nodes/OutputNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import liqp.TemplateContext;
import liqp.TemplateParser;
import liqp.exceptions.LiquidException;
import org.jsoup.internal.StringUtil;

import java.util.ArrayList;
Expand All @@ -11,12 +12,14 @@ public class OutputNode implements LNode {

private LNode expression;
private String unparsed;
private Integer unparsedline;
private Integer unparsedPosition;
private List<FilterNode> filters;

public OutputNode(LNode expression, String unparsed, Integer unparsedPosition) {
public OutputNode(LNode expression, String unparsed, Integer unparsedline, Integer unparsedPosition) {
this.expression = expression;
this.unparsed = unparsed;
this.unparsedline = unparsedline;
this.unparsedPosition = unparsedPosition;
this.filters = new ArrayList<>();
}
Expand All @@ -39,7 +42,13 @@ public Object render(TemplateContext context) {
if (localUnparsed.length() > 30) {
localUnparsed = localUnparsed.substring(0, 30) + "...";
}
context.addError(new RuntimeException("unexpected output: " + localUnparsed + " at position " + unparsedPosition));
if (unparsedline == null) {
unparsedline = -1;
}
if (unparsedPosition == null) {
unparsedPosition = -1;
}
context.addError(new LiquidException("unexpected output: " + localUnparsed, unparsedline, unparsedPosition, null));
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/liqp/parser/Flavor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public enum Flavor {
LIQUID("snippets",
Filters.DEFAULT_FILTERS,
Insertions.STANDARD_INSERTIONS,
TemplateParser.ErrorMode.LAX,
TemplateParser.ErrorMode.STRICT,
true,
true,
false
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/liqp/parser/v4/NodeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -532,15 +532,17 @@ public LNode visitFilename(FilenameContext ctx) {
public LNode visitOutput(OutputContext ctx) {
OutputNode node;
if (ctx.evaluate != null) {
node = new OutputNode(visit(ctx.expr()), null, null);
node = new OutputNode(visit(ctx.expr()), null, null, null);
} else {
String unparsed = null;
Integer unparsedLine = null;
Integer unparsedStart = null;
if (ctx.unparsed != null) {
unparsed = ctx.unparsed.getText();
unparsedStart = ctx.unparsed.getStart().getStartIndex();
unparsedLine = ctx.unparsed.getStart().getLine();
unparsedStart = ctx.unparsed.getStart().getCharPositionInLine();
}
node = new OutputNode(visit(ctx.term()), unparsed, unparsedStart);
node = new OutputNode(visit(ctx.term()), unparsed, unparsedLine, unparsedStart);
}

for (FilterContext child : ctx.filter()) {
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/liqp/nodes/GtNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
import static junit.framework.TestCase.assertEquals;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import liqp.exceptions.LiquidException;
import liqp.parser.Flavor;
import org.antlr.v4.runtime.RecognitionException;
import org.junit.Test;

Expand Down Expand Up @@ -80,4 +85,31 @@ public void testComparableTypes() {
value = TemplateParser.DEFAULT.parse("{% if a >= c %}yes{% else %}no{% endif %}").render(data);
assertEquals("no", value);
}

@Test
public void testBug267AsLiquid() {
try {
new TemplateParser.Builder().withFlavor(Flavor.LIQUID).build().parse("{{ 98 > 97 }}").render();
fail();
} catch (LiquidException e) {
assertTrue(e.getMessage().contains("parser error"));
} catch (Exception e) {
fail();
}
}

@Test
public void testBug267AsJekyll() {
Template.ContextHolder contextHolder = new Template.ContextHolder();
String res = new TemplateParser.Builder()
.withFlavor(Flavor.JEKYLL)
.build()
.parse("{{ 98 > 97 }}")
.withContextHolder(contextHolder)
.render();
assertEquals("98", res);
List<Exception> errors = contextHolder.getContext().errors();
assertEquals(1, errors.size());
assertTrue(errors.get(0).getMessage().contains("unexpected output"));
}
}

0 comments on commit 58289f2

Please sign in to comment.