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 1c5f291
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
27 changes: 15 additions & 12 deletions src/main/java/com/hubspot/jinjava/el/ExpressionResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,10 @@
import com.google.common.collect.ImmutableMap;
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.el.ext.NamedParameter;
import com.hubspot.jinjava.interpret.CollectionTooBigException;
import com.hubspot.jinjava.interpret.DeferredValueException;
import com.hubspot.jinjava.interpret.DisabledException;
import com.hubspot.jinjava.interpret.IndexOutOfRangeException;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.InvalidArgumentException;
import com.hubspot.jinjava.interpret.InvalidInputException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.OutputTooBigException;
import com.hubspot.jinjava.interpret.TemplateError;
import com.hubspot.jinjava.interpret.*;
import com.hubspot.jinjava.interpret.TemplateError.ErrorItem;
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;
import com.hubspot.jinjava.interpret.TemplateError.ErrorType;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.interpret.UnknownTokenException;
import com.hubspot.jinjava.interpret.errorcategory.BasicTemplateErrorCategory;
import com.hubspot.jinjava.lib.fn.ELFunctionDefinition;
import com.hubspot.jinjava.util.WhitespaceUtils;
Expand Down Expand Up @@ -188,6 +177,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 1c5f291

Please sign in to comment.