Skip to content
Merged
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
160 changes: 87 additions & 73 deletions src/main/java/com/hubspot/jinjava/el/ExpressionResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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.TemplateError.ErrorItem;
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;
Expand Down Expand Up @@ -154,79 +155,7 @@ private Object resolveExpression(String expression, boolean addToResolvedExpress
)
);
} catch (ELException e) {
if (e.getCause() != null && e.getCause() instanceof DeferredValueException) {
throw (DeferredValueException) e.getCause();
}
if (e.getCause() != null && e.getCause() instanceof TemplateSyntaxException) {
interpreter.addError(
TemplateError.fromException((TemplateSyntaxException) e.getCause())
);
} else if (e.getCause() != null && e.getCause() instanceof InvalidInputException) {
interpreter.addError(
TemplateError.fromInvalidInputException((InvalidInputException) e.getCause())
);
} else if (
e.getCause() != null && e.getCause() instanceof InvalidArgumentException
) {
interpreter.addError(
TemplateError.fromInvalidArgumentException(
(InvalidArgumentException) e.getCause()
)
);
} else if (
e.getCause() != null && e.getCause() instanceof CollectionTooBigException
) {
interpreter.addError(
new TemplateError(
ErrorType.FATAL,
ErrorReason.COLLECTION_TOO_BIG,
e.getCause().getMessage(),
null,
interpreter.getLineNumber(),
interpreter.getPosition(),
e
)
);
// rethrow because this is a hard limit and it will likely only happen in loops that we need to terminate
throw e;
} else if (
e.getCause() != null && e.getCause() instanceof IndexOutOfRangeException
) {
interpreter.addError(
new TemplateError(
ErrorType.WARNING,
ErrorReason.EXCEPTION,
ErrorItem.FUNCTION,
e.getMessage(),
null,
interpreter.getLineNumber(),
interpreter.getPosition(),
e
)
);
} else {
String originatingException = getRootCauseMessage(e);
final String combinedMessage = String.format(
"%s%nOriginating Exception:%n%s",
e.getMessage(),
originatingException
);
interpreter.addError(
TemplateError.fromException(
new TemplateSyntaxException(
expression,
(
e.getCause() == null ||
StringUtils.endsWith(originatingException, e.getCause().getMessage())
)
? e.getMessage()
: combinedMessage,
interpreter.getLineNumber(),
e
)
)
);
}
handleELException(expression, e);
} catch (DisabledException e) {
interpreter.addError(
new TemplateError(
Expand Down Expand Up @@ -269,6 +198,91 @@ private Object resolveExpression(String expression, boolean addToResolvedExpress
return null;
}

private void handleELException(String expression, ELException e) {
if (e.getCause() != null && e.getCause() instanceof DeferredValueException) {
throw (DeferredValueException) e.getCause();
}
if (e.getCause() != null && e.getCause() instanceof TemplateSyntaxException) {
interpreter.addError(
TemplateError.fromException((TemplateSyntaxException) e.getCause())
);
} else if (e.getCause() != null && e.getCause() instanceof InvalidInputException) {
interpreter.addError(
TemplateError.fromInvalidInputException((InvalidInputException) e.getCause())
);
} else if (e.getCause() != null && e.getCause() instanceof InvalidArgumentException) {
interpreter.addError(
TemplateError.fromInvalidArgumentException(
(InvalidArgumentException) e.getCause()
)
);
} else if (
e.getCause() != null && e.getCause() instanceof CollectionTooBigException
) {
interpreter.addError(
new TemplateError(
ErrorType.FATAL,
ErrorReason.COLLECTION_TOO_BIG,
e.getCause().getMessage(),
null,
interpreter.getLineNumber(),
interpreter.getPosition(),
e
)
);
// rethrow because this is a hard limit and it will likely only happen in loops that we need to terminate
throw e;
} else if (e.getCause() != null && e.getCause() instanceof IndexOutOfRangeException) {
interpreter.addError(
new TemplateError(
ErrorType.WARNING,
ErrorReason.EXCEPTION,
ErrorItem.FUNCTION,
e.getMessage(),
null,
interpreter.getLineNumber(),
interpreter.getPosition(),
e
)
);
} else if (e.getCause() != null && e.getCause() instanceof OutputTooBigException) {
interpreter.addError(
new TemplateError(
ErrorType.FATAL,
ErrorReason.OUTPUT_TOO_BIG,
ErrorItem.FUNCTION,
e.getCause().getMessage(),
null,
interpreter.getLineNumber(),
interpreter.getPosition(),
e
)
);
Comment on lines +248 to +260
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New case

} else {
String originatingException = getRootCauseMessage(e);
final String combinedMessage = String.format(
"%s%nOriginating Exception:%n%s",
e.getMessage(),
originatingException
);
interpreter.addError(
TemplateError.fromException(
new TemplateSyntaxException(
expression,
(
e.getCause() == null ||
StringUtils.endsWith(originatingException, e.getCause().getMessage())
)
? e.getMessage()
: combinedMessage,
interpreter.getLineNumber(),
e
)
)
);
}
}

private void validateResult(Object result) {
if (result instanceof NamedParameter) {
throw new ELException(
Expand Down