Skip to content

Commit

Permalink
Add InvalidInput error when ArithmeticException is thrown when resolv…
Browse files Browse the repository at this point in the history
…ing expression
  • Loading branch information
manheychiu1 committed Jun 5, 2024
1 parent 8ff186e commit 2cae105
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/hubspot/jinjava/el/ExpressionResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ private Object resolveExpression(String expression, boolean addToResolvedExpress
interpreter.addError(TemplateError.fromInvalidInputException(e));
} catch (InvalidArgumentException e) {
interpreter.addError(TemplateError.fromInvalidArgumentException(e));
} catch (ArithmeticException e) {
interpreter.addError(
TemplateError.fromInvalidInputException(
new InvalidInputException(
interpreter,
ExpressionResolver.class.getName(),
String.format(
"ArithmeticException when resolving expression [%s]: " +
getRootCauseMessage(e),
expression
)
)
)
);
} catch (Exception e) {
interpreter.addError(
TemplateError.fromException(
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/com/hubspot/jinjava/el/ExpressionResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,17 @@ public void itAddsErrorRenderingUnclosedExpression() {
);
}

@Test
public void itAddsInvalidInputErrorWhenArithmeticExceptionIsThrown() {
String render = interpreter.render("{% set n = 12/0|round %}{{n}}");
assertThat(interpreter.getErrors().get(0).getMessage())
.contains(
"ArithmeticException when resolving expression [[ 12/0|round ]]: ArithmeticException: / by zero"
);
assertThat(interpreter.getErrors().get(0).getReason())
.isEqualTo(ErrorReason.INVALID_INPUT);
}

public String result(String value, TestClass testClass) {
testClass.touch();
return value;
Expand Down

0 comments on commit 2cae105

Please sign in to comment.