Skip to content

Commit 655d310

Browse files
Add InvalidInput error when ArithmeticException is thrown when resolving expression (#1190)
Currently, when an `ArithmeticException` is thrown when resolving expression, we are adding a `TemplateError` with `ErrorReason.EXCEPTION` to the interpreter. Since `ArithmeticException` is caused by users' input errors, this PR changes to add a `TemplateError` with `ErrorReason.INVALID_INPUT` when `ArithmeticException` is caught. Co-authored-by: Manhey Chiu <mchiu@hubspot.com>
1 parent 8ff186e commit 655d310

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/main/java/com/hubspot/jinjava/el/ExpressionResolver.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,20 @@ private Object resolveExpression(String expression, boolean addToResolvedExpress
188188
interpreter.addError(TemplateError.fromInvalidInputException(e));
189189
} catch (InvalidArgumentException e) {
190190
interpreter.addError(TemplateError.fromInvalidArgumentException(e));
191+
} catch (ArithmeticException e) {
192+
interpreter.addError(
193+
TemplateError.fromInvalidInputException(
194+
new InvalidInputException(
195+
interpreter,
196+
ExpressionResolver.class.getName(),
197+
String.format(
198+
"ArithmeticException when resolving expression [%s]: " +
199+
getRootCauseMessage(e),
200+
expression
201+
)
202+
)
203+
)
204+
);
191205
} catch (Exception e) {
192206
interpreter.addError(
193207
TemplateError.fromException(

src/test/java/com/hubspot/jinjava/el/ExpressionResolverTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,17 @@ public void itAddsErrorRenderingUnclosedExpression() {
664664
);
665665
}
666666

667+
@Test
668+
public void itAddsInvalidInputErrorWhenArithmeticExceptionIsThrown() {
669+
String render = interpreter.render("{% set n = 12/0|round %}{{n}}");
670+
assertThat(interpreter.getErrors().get(0).getMessage())
671+
.contains(
672+
"ArithmeticException when resolving expression [[ 12/0|round ]]: ArithmeticException: / by zero"
673+
);
674+
assertThat(interpreter.getErrors().get(0).getReason())
675+
.isEqualTo(ErrorReason.INVALID_INPUT);
676+
}
677+
667678
public String result(String value, TestClass testClass) {
668679
testClass.touch();
669680
return value;

0 commit comments

Comments
 (0)