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 decfa55
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 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
19 changes: 12 additions & 7 deletions src/test/java/com/hubspot/jinjava/el/ExpressionResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@
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;
Expand Down Expand Up @@ -664,6 +658,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 decfa55

Please sign in to comment.