Skip to content
This repository has been archived by the owner on Sep 15, 2018. It is now read-only.

Commit

Permalink
change invalid tag use errors to syntax errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredstehler committed Oct 21, 2015
1 parent e12ef64 commit a203c5f
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 84 deletions.
28 changes: 14 additions & 14 deletions src/main/java/com/hubspot/jinjava/lib/filter/AbsFilter.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**********************************************************************
Copyright (c) 2014 HubSpot Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* Copyright (c) 2014 HubSpot Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**********************************************************************/
package com.hubspot.jinjava.lib.filter;

Expand Down Expand Up @@ -66,7 +66,7 @@ public Object filter(Object object, JinjavaInterpreter interpreter, String... ar
try {
return new BigDecimal((String) object).abs();
} catch (Exception e) {
throw new InterpretException(object + " can't be dealed with abs filter", e);
throw new InterpretException(object + " can't be handled by abs filter", e);
}
}
return object;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/hubspot/jinjava/lib/tag/BlockTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.tree.TagNode;
import com.hubspot.jinjava.tree.output.BlockPlaceholderOutputNode;
import com.hubspot.jinjava.tree.output.OutputNode;
Expand Down Expand Up @@ -49,7 +49,7 @@ public class BlockTag implements Tag {
public OutputNode interpretOutput(TagNode tagNode, JinjavaInterpreter interpreter) {
HelperStringTokenizer tagData = new HelperStringTokenizer(tagNode.getHelpers());
if (!tagData.hasNext()) {
throw new InterpretException("Tag 'block' expects an identifier", tagNode.getLineNumber());
throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'block' expects an identifier", tagNode.getLineNumber());
}

String blockName = WhitespaceUtils.unquote(tagData.next());
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/com/hubspot/jinjava/lib/tag/CycleTag.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**********************************************************************
Copyright (c) 2014 HubSpot Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* Copyright (c) 2014 HubSpot Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**********************************************************************/
package com.hubspot.jinjava.lib.tag;

Expand All @@ -20,8 +20,8 @@
import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.tree.TagNode;
import com.hubspot.jinjava.util.HelperStringTokenizer;

Expand Down Expand Up @@ -89,7 +89,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
interpreter.getContext().put(var, values);
return "";
} else {
throw new InterpretException("Tag 'cycle' expects 1 or 3 helper(s) >>> " + helper.size(), tagNode.getLineNumber());
throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'cycle' expects 1 or 3 helper(s), was: " + helper.size(), tagNode.getLineNumber());
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/ExtendsTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.tree.Node;
import com.hubspot.jinjava.tree.TagNode;
import com.hubspot.jinjava.util.HelperStringTokenizer;
Expand Down Expand Up @@ -81,7 +82,7 @@ public class ExtendsTag implements Tag {
public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
HelperStringTokenizer tokenizer = new HelperStringTokenizer(tagNode.getHelpers());
if (!tokenizer.hasNext()) {
throw new InterpretException("Tag 'extends' expects template path", tagNode.getLineNumber());
throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'extends' expects template path", tagNode.getLineNumber());
}

String path = interpreter.resolveString(tokenizer.next(), tagNode.getLineNumber());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/ForTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.tree.Node;
import com.hubspot.jinjava.tree.TagNode;
import com.hubspot.jinjava.util.ForLoop;
Expand Down Expand Up @@ -86,7 +87,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
}

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

String loopExpr = StringUtils.join(helper.subList(inPos + 1, helper.size()), ",");
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.lib.fn.MacroFunction;
import com.hubspot.jinjava.tree.Node;
import com.hubspot.jinjava.tree.TagNode;
Expand All @@ -31,13 +32,11 @@
"{% endmacro %}\n" +
"{% macro footer(tag, footer_text) %}\n" +
" <footer> <{{ tag }}>{{ footer_text }} </{{tag}}> </footer>\n" +
"{% endmacro %}"
),
"{% endmacro %}"),
@JinjavaSnippet(
desc = "The macro html file is accessed from a different template, but only the footer macro is imported and executed",
code = "{% from 'custom/page/web_page_basic/my_macros.html' import footer %}\n" +
"{{ footer('h2', 'My footer info') }}"
),
"{{ footer('h2', 'My footer info') }}"),
})
public class FromTag implements Tag {

Expand All @@ -52,7 +51,7 @@ public String getName() {
public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
List<String> helper = new HelperStringTokenizer(tagNode.getHelpers()).splitComma(true).allTokens();
if (helper.size() < 3 || !helper.get(1).equals("import")) {
throw new InterpretException("Tag 'from' expects import list: " + helper, tagNode.getLineNumber());
throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'from' expects import list: " + helper, tagNode.getLineNumber());
}

String templateFile = interpreter.resolveString(helper.get(0), tagNode.getLineNumber());
Expand Down Expand Up @@ -86,8 +85,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {

if (val != null) {
interpreter.getContext().addGlobalMacro((MacroFunction) val);
}
else {
} else {
val = child.getContext().get(importMapping.getKey());

if (val != null) {
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/com/hubspot/jinjava/lib/tag/IfchangedTag.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
/**********************************************************************
Copyright (c) 2014 HubSpot Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* Copyright (c) 2014 HubSpot Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**********************************************************************/
package com.hubspot.jinjava.lib.tag;

import org.apache.commons.lang3.StringUtils;

import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.tree.Node;
import com.hubspot.jinjava.tree.TagNode;

Expand All @@ -43,7 +43,7 @@ public class IfchangedTag implements Tag {
@Override
public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
if (StringUtils.isBlank(tagNode.getHelpers())) {
throw new InterpretException("Tag 'ifchanged' expects a variable parameter", tagNode.getLineNumber());
throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'ifchanged' expects a variable parameter", tagNode.getLineNumber());
}
boolean isChanged = true;
String var = tagNode.getHelpers().trim();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.hubspot.jinjava.interpret.TemplateError;
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;
import com.hubspot.jinjava.interpret.TemplateError.ErrorType;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.lib.fn.MacroFunction;
import com.hubspot.jinjava.tree.Node;
import com.hubspot.jinjava.tree.TagNode;
Expand Down Expand Up @@ -61,7 +62,7 @@ public String getName() {
public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
List<String> helper = new HelperStringTokenizer(tagNode.getHelpers()).allTokens();
if (helper.isEmpty()) {
throw new InterpretException("Tag 'import' expects 1 helper >>> " + helper.size(), tagNode.getLineNumber());
throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'import' expects 1 helper, was: " + helper.size(), tagNode.getLineNumber());
}

String contextVar = "";
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/hubspot/jinjava/lib/tag/IncludeTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.hubspot.jinjava.interpret.TemplateError;
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;
import com.hubspot.jinjava.interpret.TemplateError.ErrorType;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.tree.Node;
import com.hubspot.jinjava.tree.TagNode;
import com.hubspot.jinjava.util.HelperStringTokenizer;
Expand All @@ -49,7 +50,7 @@ public class IncludeTag implements Tag {
public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
HelperStringTokenizer helper = new HelperStringTokenizer(tagNode.getHelpers());
if (!helper.hasNext()) {
throw new InterpretException("Tag 'include' expects template path", tagNode.getLineNumber());
throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'include' expects template path", tagNode.getLineNumber());
}

String path = StringUtils.trimToEmpty(helper.next());
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/com/hubspot/jinjava/lib/tag/MacroTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.lib.fn.MacroFunction;
import com.hubspot.jinjava.tree.TagNode;

Expand All @@ -31,8 +31,7 @@
" {{ argument_name }}\n" +
" {{ argument_name2 }}\n" +
"{% endmacro %}\n" +
"{{ name_of_macro(\"value to pass to argument 1\", \"value to pass to argument 2\") }}"
),
"{{ name_of_macro(\"value to pass to argument 1\", \"value to pass to argument 2\") }}"),
@JinjavaSnippet(
desc = "Example of a macro used to print CSS3 properties with the various vendor prefixes",
code = "{% macro trans(value) %}\n" +
Expand All @@ -41,8 +40,7 @@
" -o-transition: {{value}};\n" +
" -ms-transition: {{value}};\n" +
" transition: {{value}};\n" +
"{% endmacro %}"
),
"{% endmacro %}"),
@JinjavaSnippet(
desc = "The macro can then be called like a function. The macro is printed for anchor tags in CSS.",
code = "a { {{ trans(\"all .2s ease-in-out\") }} }"),
Expand All @@ -65,7 +63,7 @@ public String getEndTagName() {
public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
Matcher matcher = MACRO_PATTERN.matcher(tagNode.getHelpers());
if (!matcher.find()) {
throw new InterpretException("Unable to parse macro definition: " + tagNode.getHelpers());
throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Unable to parse macro definition: " + tagNode.getHelpers(), tagNode.getLineNumber());
}

String name = matcher.group(1);
Expand All @@ -90,8 +88,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {

Object argVal = interpreter.resolveELExpression(argValStr.toString(), tagNode.getLineNumber());
argNamesWithDefaults.put(argName, argVal);
}
else {
} else {
argNamesWithDefaults.put(arg, null);
}
}
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/com/hubspot/jinjava/lib/tag/SetTag.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**********************************************************************
Copyright (c) 2014 HubSpot Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* Copyright (c) 2014 HubSpot Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**********************************************************************/
package com.hubspot.jinjava.lib.tag;

Expand All @@ -22,6 +22,7 @@
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.tree.TagNode;

/**
Expand All @@ -32,7 +33,8 @@
* @author anysome
*
*/
@JinjavaDoc(value = "Assigns the value or result of a statement to a variable",
@JinjavaDoc(
value = "Assigns the value or result of a statement to a variable",
params = {
@JinjavaParam(value = "var", type = "variable identifier", desc = "The name of the variable"),
@JinjavaParam(value = "expr", type = "expression", desc = "The value stored in the variable (string, number, boolean, or sequence")
Expand All @@ -41,14 +43,12 @@
@JinjavaSnippet(
desc = "Set a variable in with a set statement and print the variable in a expression",
code = "{% set primaryColor = \"#F7761F\" %}\n" +
"{{ primaryColor }}\n"
),
"{{ primaryColor }}\n"),
@JinjavaSnippet(
desc = "You can combine multiple values or variables into a sequence variable",
code = "{% set var_one = \"String 1\" %}\n" +
"{% set var_two = \"String 2\" %}\n" +
"{% set sequence = [var_one, var_two] %}"
),
"{% set sequence = [var_one, var_two] %}"),
})
public class SetTag implements Tag {

Expand All @@ -63,7 +63,7 @@ public String getName() {
@Override
public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
if (!tagNode.getHelpers().contains("=")) {
throw new InterpretException("Tag 'set' expects an assignment expression with '=', but was: " + tagNode.getHelpers(), tagNode.getLineNumber());
throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'set' expects an assignment expression with '=', but was: " + tagNode.getHelpers(), tagNode.getLineNumber());
}

int eqPos = tagNode.getHelpers().indexOf('=');
Expand Down

0 comments on commit a203c5f

Please sign in to comment.