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 e263e83
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 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
31 changes: 14 additions & 17 deletions src/test/java/com/hubspot/jinjava/el/ExpressionResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e263e83

Please sign in to comment.