Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add InvalidInput error when ArithmeticException is thrown when resolving expression #1190

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading