diff --git a/src/main/java/com/hubspot/jinjava/el/ExpressionResolver.java b/src/main/java/com/hubspot/jinjava/el/ExpressionResolver.java index a91dfba71..da810555c 100644 --- a/src/main/java/com/hubspot/jinjava/el/ExpressionResolver.java +++ b/src/main/java/com/hubspot/jinjava/el/ExpressionResolver.java @@ -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( diff --git a/src/test/java/com/hubspot/jinjava/el/ExpressionResolverTest.java b/src/test/java/com/hubspot/jinjava/el/ExpressionResolverTest.java index 4543ee667..9d8852bf6 100644 --- a/src/test/java/com/hubspot/jinjava/el/ExpressionResolverTest.java +++ b/src/test/java/com/hubspot/jinjava/el/ExpressionResolverTest.java @@ -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;