Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

populate fieldname for TemplateSyntaxException #190

Merged
merged 1 commit into from
Apr 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/com/hubspot/jinjava/Jinjava.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.hubspot.jinjava.interpret.RenderResult;
import com.hubspot.jinjava.interpret.TemplateError;
import com.hubspot.jinjava.interpret.TemplateError.ErrorType;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.loader.ClasspathResourceLocator;
import com.hubspot.jinjava.loader.ResourceLocator;

Expand Down Expand Up @@ -198,6 +199,9 @@ public RenderResult renderForResult(String template, Map<String, ?> bindings, Ji
String result = interpreter.render(template);
return new RenderResult(result, interpreter.getContext(), interpreter.getErrors());
} catch (InterpretException e) {
if (e instanceof TemplateSyntaxException) {
return new RenderResult(TemplateError.fromException((TemplateSyntaxException) e), interpreter.getContext(), interpreter.getErrors());
}
return new RenderResult(TemplateError.fromSyntaxError(e), interpreter.getContext(), interpreter.getErrors());
} catch (Exception e) {
return new RenderResult(TemplateError.fromException(e), interpreter.getContext(), interpreter.getErrors());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/ForTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
}

if (inPos >= helper.size()) {
throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'for' expects valid 'in' clause, got: " + tagNode.getHelpers(), tagNode.getLineNumber(), tagNode.getStartPosition());
throw new TemplateSyntaxException(tagNode.getHelpers().trim(), "Tag 'for' expects valid 'in' clause, got: " + tagNode.getHelpers(), tagNode.getLineNumber(), tagNode.getStartPosition());
}

String loopExpr = StringUtils.join(helper.subList(inPos + 1, helper.size()), ",");
Expand Down
15 changes: 10 additions & 5 deletions src/test/java/com/hubspot/jinjava/interpret/TemplateErrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@

public class TemplateErrorTest {

private Context context;
private JinjavaInterpreter interpreter;
private Jinjava jinjava;

@Before
public void setup() {
interpreter = new Jinjava().newInterpreter();
jinjava = new Jinjava();
interpreter = jinjava.newInterpreter();
JinjavaInterpreter.pushCurrent(interpreter);

context = interpreter.getContext();
}


@Test
public void itShowsFriendlyNameOfBaseObjectForPropNotFound() {
TemplateError e = TemplateError.fromUnknownProperty(new Object(), "foo", 123, 4);
Expand Down Expand Up @@ -51,4 +49,11 @@ public void itRetainsFieldNameCaseForUnknownToken() {
interpreter.render("{% unKnown() %}");
assertThat(interpreter.getErrors().get(0).getFieldName()).isEqualTo("unKnown");
}

@Test
public void itSetsFieldNameCaseForSyntaxErrorInFor() {
RenderResult renderResult = jinjava.renderForResult("{% for item inna navigation %}{% endfor %}", ImmutableMap.of());
assertThat(renderResult.getErrors().get(0).getFieldName()).isEqualTo("item inna navigation");
}

}
21 changes: 10 additions & 11 deletions src/test/java/com/hubspot/jinjava/lib/tag/ForTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.junit.Test;

import com.google.common.base.Splitter;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
Expand Down Expand Up @@ -45,7 +44,7 @@ public void setup() {
}

@Test
public void forLoopUsingLoopLastVar() throws Exception {
public void forLoopUsingLoopLastVar() {
context.put("the_list", Lists.newArrayList(1L, 2L, 3L, 7L));
TagNode tagNode = (TagNode) fixture("loop-last-var");
Document dom = Jsoup.parseBodyFragment(tag.interpret(tagNode, interpreter));
Expand All @@ -54,23 +53,23 @@ public void forLoopUsingLoopLastVar() throws Exception {
}

@Test
public void forLoopUsingScalarValue() throws Exception {
public void forLoopUsingScalarValue() {
context.put("the_list", 999L);
TagNode tagNode = (TagNode) fixture("loop-with-scalar");
String output = tag.interpret(tagNode, interpreter);
assertThat(output.trim()).isEqualTo("<h3>The Number: 999</h3>");
}

@Test
public void forLoopNestedFor() throws Exception {
public void forLoopNestedFor() {
TagNode tagNode = (TagNode) fixture("nested-fors");
assertThat(Splitter.on("\n").trimResults().omitEmptyStrings().split(
tag.interpret(tagNode, interpreter)))
.contains("02", "03", "12", "13");
}

@Test
public void forLoopMultipleLoopVars() throws Exception {
public void forLoopMultipleLoopVars() {
Map<String, Object> dict = Maps.newHashMap();
dict.put("foo", "one");
dict.put("bar", 2L);
Expand All @@ -83,7 +82,7 @@ public void forLoopMultipleLoopVars() throws Exception {
}

@Test
public void forLoopMultipleLoopVarsArbitraryNames() throws Exception {
public void forLoopMultipleLoopVarsArbitraryNames() {
Map<String, Object> dict = ImmutableMap.of(
"grand", "ol'",
"adserving", "team");
Expand All @@ -99,13 +98,13 @@ public void forLoopMultipleLoopVarsArbitraryNames() throws Exception {
}

@Test
public void forLoopLiteralLoopExpr() throws Exception {
public void forLoopLiteralLoopExpr() {
TagNode tagNode = (TagNode) fixture("literal-loop-expr");
assertThat(tag.interpret(tagNode, interpreter)).isEqualTo("012345");
}

@Test
public void forLoopWithNestedCycle() throws Exception {
public void forLoopWithNestedCycle() {
context.put("cycle1", "odd");
context.put("cycle2", "even");

Expand All @@ -115,13 +114,13 @@ public void forLoopWithNestedCycle() throws Exception {
}

@Test
public void forLoopIndexVar() throws Exception {
public void forLoopIndexVar() {
TagNode tagNode = (TagNode) fixture("loop-index-var");
assertThat(tag.interpret(tagNode, interpreter)).isEqualTo("012345");
}

@Test
public void forLoopSupportsAllLoopVarsInHublDocs() throws Exception {
public void forLoopSupportsAllLoopVarsInHublDocs() {
TagNode tagNode = (TagNode) fixture("hubl-docs-loop-vars");
Document dom = Jsoup.parseBodyFragment(tag.interpret(tagNode, interpreter));

Expand Down Expand Up @@ -200,7 +199,7 @@ private Node fixture(String name) {
Resources.getResource(String.format("tags/fortag/%s.jinja", name)), StandardCharsets.UTF_8))
.buildTree().getChildren().getFirst();
} catch (IOException e) {
throw Throwables.propagate(e);
throw new RuntimeException(e);
}
}

Expand Down