From e263e839bca4354229cd3255aae170709984fc1f Mon Sep 17 00:00:00 2001 From: Manhey Chiu Date: Wed, 5 Jun 2024 08:09:30 +0100 Subject: [PATCH] Add InvalidInput error when ArithmeticException is thrown when resolving expression --- .../jinjava/el/ExpressionResolver.java | 14 +++++++++ .../jinjava/el/ExpressionResolverTest.java | 31 +++++++++---------- 2 files changed, 28 insertions(+), 17 deletions(-) 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..0c77d9751 100644 --- a/src/test/java/com/hubspot/jinjava/el/ExpressionResolverTest.java +++ b/src/test/java/com/hubspot/jinjava/el/ExpressionResolverTest.java @@ -3,31 +3,17 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; -import com.google.common.collect.ForwardingList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; +import com.google.common.collect.*; import com.hubspot.jinjava.Jinjava; import com.hubspot.jinjava.JinjavaConfig; -import com.hubspot.jinjava.interpret.Context; +import com.hubspot.jinjava.interpret.*; import com.hubspot.jinjava.interpret.Context.Library; -import com.hubspot.jinjava.interpret.JinjavaInterpreter; -import com.hubspot.jinjava.interpret.LazyExpression; -import com.hubspot.jinjava.interpret.RenderResult; -import com.hubspot.jinjava.interpret.TemplateError; import com.hubspot.jinjava.interpret.TemplateError.ErrorItem; import com.hubspot.jinjava.interpret.TemplateError.ErrorReason; import com.hubspot.jinjava.objects.PyWrapper; import com.hubspot.jinjava.objects.date.PyishDate; import java.math.BigDecimal; -import java.util.Collection; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.function.Supplier; import org.junit.Before; import org.junit.Test; @@ -664,6 +650,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;