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

WIP Error and documentation overhaul #289

Merged
merged 6 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
More documentation changes.
  • Loading branch information
Matt Coley committed Feb 27, 2019
commit 1c6276ddb56cff3c7c8a541393d7d2364f549a7d
3 changes: 3 additions & 0 deletions src/main/java/com/hubspot/jinjava/Jinjava.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.hubspot.jinjava.interpret.FatalTemplateErrorsException;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.InvalidArgumentException;
import com.hubspot.jinjava.interpret.InvalidInputException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.RenderResult;
import com.hubspot.jinjava.interpret.TemplateError;
Expand Down Expand Up @@ -206,6 +207,8 @@ public RenderResult renderForResult(String template, Map<String, ?> bindings, Ji
return new RenderResult(TemplateError.fromSyntaxError(e), interpreter.getContext(), interpreter.getErrorsCopy());
} catch (InvalidArgumentException e) {
return new RenderResult(TemplateError.fromInvalidArgumentException(e), interpreter.getContext(), interpreter.getErrorsCopy());
} catch (InvalidInputException e) {
return new RenderResult(TemplateError.fromInvalidInputException(e), interpreter.getContext(), interpreter.getErrorsCopy());
}
catch (Exception e) {
return new RenderResult(TemplateError.fromException(e), interpreter.getContext(), interpreter.getErrorsCopy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

String value() default "";

JinjavaParam input();
JinjavaParam[] input() default {};

JinjavaParam[] params() default {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class InvalidArgumentException extends RuntimeException {
private final int lineNumber;
private final int startPosition;
private final String message;
private final String name;

public InvalidArgumentException(JinjavaInterpreter interpreter, Importable importable, InvalidReason invalidReason, int argumentNumber, Object... errorMessageArgs) {
this.message = String.format("Invalid argument in '%s': %s",
Expand All @@ -15,6 +16,7 @@ public InvalidArgumentException(JinjavaInterpreter interpreter, Importable impor

this.lineNumber = interpreter.getLineNumber();
this.startPosition = interpreter.getPosition();
this.name = importable.getName();
}

public String getMessage() {
Expand All @@ -29,6 +31,10 @@ public int getStartPosition() {
return startPosition;
}

public String getName() {
return name;
}

private static String formatArgumentNumber(int argumentNumber) {
switch (argumentNumber){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not very i18nable, but in any case, you can do this for all numbers by just looking at the last digit of the int and using "st" if 1, "nd" if 2, "rd" if 3 and "th" for all other values.

case 1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class InvalidInputException extends RuntimeException {
private final int lineNumber;
private final int startPosition;
private final String message;
private final String name;

public InvalidInputException(JinjavaInterpreter interpreter, Importable importable, InvalidReason invalidReason, Object... errorMessageArgs) {
this.message = String.format("Invalid input in '%s': input variable %s",
Expand All @@ -15,6 +16,7 @@ public InvalidInputException(JinjavaInterpreter interpreter, Importable importab

this.lineNumber = interpreter.getLineNumber();
this.startPosition = interpreter.getPosition();
this.name = importable.getName();
}

public String getMessage() {
Expand All @@ -28,4 +30,8 @@ public int getLineNumber() {
public int getStartPosition() {
return startPosition;
}

public String getName() {
return name;
}
}
14 changes: 13 additions & 1 deletion src/main/java/com/hubspot/jinjava/interpret/TemplateError.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum ErrorReason {
MISSING,
DISABLED,
INVALID_ARGUMENT,
INVALID_INPUT,
OTHER
}

Expand Down Expand Up @@ -73,7 +74,18 @@ public static TemplateError fromInvalidArgumentException(InvalidArgumentExceptio
ErrorReason.INVALID_ARGUMENT,
ErrorItem.PROPERTY,
ex.getMessage(),
ex.getFieldName(),
ex.getName(),
ex.getLineNumber(),
ex.getStartPosition(),
ex);
}

public static TemplateError fromInvalidInputException(InvalidInputException ex) {
return new TemplateError(ErrorType.FATAL,
ErrorReason.INVALID_INPUT,
ErrorItem.PROPERTY,
ex.getMessage(),
ex.getName(),
ex.getLineNumber(),
ex.getStartPosition(),
ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@

import java.util.Objects;

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.JinjavaInterpreter;
import com.hubspot.jinjava.util.ForLoop;
import com.hubspot.jinjava.util.ObjectIterator;

@JinjavaDoc(
value = "Returns true if a list contains all values in a second list",
input = @JinjavaParam(value = "list", type="list"),
params = @JinjavaParam(value = "list_two", type="list", desc = "The second list to check if every element is in the first list"),
snippets = {
@JinjavaSnippet(
code = "{{ [1, 2, 3] is containingall [2, 3] }}")
})
public class IsContainingAllExpTest implements ExpTest {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@

import java.util.Objects;

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.JinjavaInterpreter;
import com.hubspot.jinjava.util.ForLoop;
import com.hubspot.jinjava.util.ObjectIterator;

@JinjavaDoc(
value = "Returns true if a list contains a value",
input = @JinjavaParam(value = "list", type = "list"),
params = @JinjavaParam(value = "value", type = "object", desc = "The value to check is in the list"),
snippets = {
@JinjavaSnippet(
code = "{{ [1, 2, 3] is containing 2 }}")
})
public class IsContainingExpTest implements ExpTest {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hubspot.jinjava.lib.exptest;

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.JinjavaInterpreter;

@JinjavaDoc(
value = "Return true if the variable is defined",
input = @JinjavaParam(value = "value", type = "object"),
snippets = {
@JinjavaSnippet(
code = "{% if variable is defined %}\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.InvalidArgumentException;
import com.hubspot.jinjava.interpret.InvalidReason;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;

@JinjavaDoc(value = "Check if a variable is divisible by a number",
params = {
@JinjavaParam(value = "num", type = "number", desc = "The number to check whether a number is divisble by")
},
@JinjavaDoc(
value = "Returns true if a variable is divisible by a number",
input = @JinjavaParam(value = "num", type = "number"),
params = @JinjavaParam(value = "divisor", type = "number", desc = "The number to check whether a number is divisible by"),
snippets = {
@JinjavaSnippet(
code = "{% if variable is divisbleby 5 %}\n" +
Expand Down Expand Up @@ -44,10 +45,7 @@ public boolean evaluate(Object var, JinjavaInterpreter interpreter, Object... ar
}

if (!Number.class.isAssignableFrom(args[0].getClass())) {
throw new InvalidArgumentException(interpreter,
getName(),
String.format(InvalidArgumentException.NUMBER_FORMAT_EXCEPTION_TEMPLATE, "num", args[0].toString()),
"num");
throw new InvalidArgumentException(interpreter, this, InvalidReason.NUMBER_FORMAT, 0, args[0].toString());
}

return ((Number) var).intValue() % ((Number) args[0]).intValue() == 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import de.odysseus.el.misc.TypeConverter;

@JinjavaDoc(
value = "Check if an object has the same value as another object",
value = "Returns true if an object has the same value as another object",
input = @JinjavaParam(value = "first", type = "object"),
params = {
@JinjavaParam(value = "other", type = "object", desc = "Another object to check equality against")
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hubspot.jinjava.lib.exptest;

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.JinjavaInterpreter;

@JinjavaDoc(
value = "Return true if the value is even",
value = "Returns true if the value is even",
input = @JinjavaParam(value = "num", type = "number"),
snippets = {
@JinjavaSnippet(
code = "{% if variable is even %}\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hubspot.jinjava.lib.exptest;

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.JinjavaInterpreter;

@JinjavaDoc(
value = "Return true if the object is iterable (sequence, dict, etc)",
input = @JinjavaParam(value = "object", type = "object"),
snippets = {
@JinjavaSnippet(
code = "{% if variable is iterable %}\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import org.apache.commons.lang3.StringUtils;

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.JinjavaInterpreter;

@JinjavaDoc(
value = "Return true if the given string is all lowercased",
value = "Return true if the given string is all lowercase",
input = @JinjavaParam(value = "string", type = "string"),
snippets = {
@JinjavaSnippet(
code = "{% if variable is lower %}\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import java.util.Map;

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.JinjavaInterpreter;

@JinjavaDoc(
value = "Return true if the given object is a dict",
input = @JinjavaParam(value = "object", type = "object"),
snippets = {
@JinjavaSnippet(
code = "{% if variable is mapping %}\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hubspot.jinjava.lib.exptest;

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.JinjavaInterpreter;

@JinjavaDoc(
value = "Return true if the given object is null / none",
input = @JinjavaParam(value = "object", type = "object"),
snippets = {
@JinjavaSnippet(
code = "{% unless variable is none %}\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hubspot.jinjava.lib.exptest;

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.JinjavaInterpreter;

@JinjavaDoc(
value = "Return true if the object is a number",
input = @JinjavaParam(value = "object", type = "object"),
snippets = {
@JinjavaSnippet(
code = "{% if variable is number %}\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hubspot.jinjava.lib.exptest;

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.JinjavaInterpreter;

@JinjavaDoc(
value = "Return true if the object is an odd number",
value = "Return true if a number is an odd number",
input = @JinjavaParam(value = "num", type = "number"),
snippets = {
@JinjavaSnippet(
code = "{% if variable is odd %}\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;

@JinjavaDoc(value = "Return true if variable is pointing at same object as other variable",
@JinjavaDoc(
value = "Return true if variable is pointing at same object as other variable",
input = @JinjavaParam(value = "object", type = "object"),
params = @JinjavaParam(value = "other", type = "object", desc = "A second object to check the variables value against"),
snippets = {
@JinjavaSnippet(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hubspot.jinjava.lib.exptest;

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.JinjavaInterpreter;

@JinjavaDoc(
value = "Return true if the variable is a sequence. Sequences are variables that are iterable.",
input = @JinjavaParam(value = "object", type = "object"),
snippets = {
@JinjavaSnippet(
code = "{% if variable is sequence %}\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

@JinjavaDoc(
value = "Return true if object is a string which contains a specified other string",
params = @JinjavaParam(value = "string", type = "string", desc = "A second string to check is contained in the first string"),
input = @JinjavaParam(value = "string", type = "string"),
params = @JinjavaParam(value = "check", type = "string", desc = "A second string to check is contained in the first string"),
snippets = {
@JinjavaSnippet(
code = "{% if variable is string_containing 'foo' %}\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hubspot.jinjava.lib.exptest;

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.JinjavaInterpreter;

@JinjavaDoc(
value = "Return true if object is a string",
input = @JinjavaParam(value = "value", type = "object"),
snippets = {
@JinjavaSnippet(
code = "{% if variable is string %}\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.hubspot.jinjava.lib.exptest;

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;

@JinjavaDoc(
value = "Return true if object is a string which starts with a specified other string",
input = @JinjavaParam(value = "string", type = "string"),
params = @JinjavaParam(value = "check", type = "string", desc = "A second string to check is the start of the first string"),
snippets = {
@JinjavaSnippet(
code = "{% if variable is string_startingwith 'foo' %}\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.hubspot.jinjava.lib.exptest;

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.JinjavaInterpreter;
import com.hubspot.jinjava.util.ObjectTruthValue;

@JinjavaDoc(
value = "Return true if object is 'truthy'",
input = @JinjavaParam(value = "value", type = "object"),
snippets = {
@JinjavaSnippet(
code = "{% if variable is truthy %}\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hubspot.jinjava.lib.exptest;

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.JinjavaInterpreter;

@JinjavaDoc(
value = "Return true if object is undefined",
input = @JinjavaParam(value = "value", type = "object"),
snippets = {
@JinjavaSnippet(
code = "{% if variable is undefined %}\n" +
Expand Down
Loading