Skip to content

Commit b60d218

Browse files
committed
Move ELException handling to separate method
1 parent 8dee422 commit b60d218

1 file changed

Lines changed: 73 additions & 73 deletions

File tree

src/main/java/com/hubspot/jinjava/el/ExpressionResolver.java

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -154,79 +154,7 @@ private Object resolveExpression(String expression, boolean addToResolvedExpress
154154
)
155155
);
156156
} catch (ELException e) {
157-
if (e.getCause() != null && e.getCause() instanceof DeferredValueException) {
158-
throw (DeferredValueException) e.getCause();
159-
}
160-
if (e.getCause() != null && e.getCause() instanceof TemplateSyntaxException) {
161-
interpreter.addError(
162-
TemplateError.fromException((TemplateSyntaxException) e.getCause())
163-
);
164-
} else if (e.getCause() != null && e.getCause() instanceof InvalidInputException) {
165-
interpreter.addError(
166-
TemplateError.fromInvalidInputException((InvalidInputException) e.getCause())
167-
);
168-
} else if (
169-
e.getCause() != null && e.getCause() instanceof InvalidArgumentException
170-
) {
171-
interpreter.addError(
172-
TemplateError.fromInvalidArgumentException(
173-
(InvalidArgumentException) e.getCause()
174-
)
175-
);
176-
} else if (
177-
e.getCause() != null && e.getCause() instanceof CollectionTooBigException
178-
) {
179-
interpreter.addError(
180-
new TemplateError(
181-
ErrorType.FATAL,
182-
ErrorReason.COLLECTION_TOO_BIG,
183-
e.getCause().getMessage(),
184-
null,
185-
interpreter.getLineNumber(),
186-
interpreter.getPosition(),
187-
e
188-
)
189-
);
190-
// rethrow because this is a hard limit and it will likely only happen in loops that we need to terminate
191-
throw e;
192-
} else if (
193-
e.getCause() != null && e.getCause() instanceof IndexOutOfRangeException
194-
) {
195-
interpreter.addError(
196-
new TemplateError(
197-
ErrorType.WARNING,
198-
ErrorReason.EXCEPTION,
199-
ErrorItem.FUNCTION,
200-
e.getMessage(),
201-
null,
202-
interpreter.getLineNumber(),
203-
interpreter.getPosition(),
204-
e
205-
)
206-
);
207-
} else {
208-
String originatingException = getRootCauseMessage(e);
209-
final String combinedMessage = String.format(
210-
"%s%nOriginating Exception:%n%s",
211-
e.getMessage(),
212-
originatingException
213-
);
214-
interpreter.addError(
215-
TemplateError.fromException(
216-
new TemplateSyntaxException(
217-
expression,
218-
(
219-
e.getCause() == null ||
220-
StringUtils.endsWith(originatingException, e.getCause().getMessage())
221-
)
222-
? e.getMessage()
223-
: combinedMessage,
224-
interpreter.getLineNumber(),
225-
e
226-
)
227-
)
228-
);
229-
}
157+
handleELException(expression, e);
230158
} catch (DisabledException e) {
231159
interpreter.addError(
232160
new TemplateError(
@@ -269,6 +197,78 @@ private Object resolveExpression(String expression, boolean addToResolvedExpress
269197
return null;
270198
}
271199

200+
private void handleELException(String expression, ELException e) {
201+
if (e.getCause() != null && e.getCause() instanceof DeferredValueException) {
202+
throw (DeferredValueException) e.getCause();
203+
}
204+
if (e.getCause() != null && e.getCause() instanceof TemplateSyntaxException) {
205+
interpreter.addError(
206+
TemplateError.fromException((TemplateSyntaxException) e.getCause())
207+
);
208+
} else if (e.getCause() != null && e.getCause() instanceof InvalidInputException) {
209+
interpreter.addError(
210+
TemplateError.fromInvalidInputException((InvalidInputException) e.getCause())
211+
);
212+
} else if (e.getCause() != null && e.getCause() instanceof InvalidArgumentException) {
213+
interpreter.addError(
214+
TemplateError.fromInvalidArgumentException(
215+
(InvalidArgumentException) e.getCause()
216+
)
217+
);
218+
} else if (
219+
e.getCause() != null && e.getCause() instanceof CollectionTooBigException
220+
) {
221+
interpreter.addError(
222+
new TemplateError(
223+
ErrorType.FATAL,
224+
ErrorReason.COLLECTION_TOO_BIG,
225+
e.getCause().getMessage(),
226+
null,
227+
interpreter.getLineNumber(),
228+
interpreter.getPosition(),
229+
e
230+
)
231+
);
232+
// rethrow because this is a hard limit and it will likely only happen in loops that we need to terminate
233+
throw e;
234+
} else if (e.getCause() != null && e.getCause() instanceof IndexOutOfRangeException) {
235+
interpreter.addError(
236+
new TemplateError(
237+
ErrorType.WARNING,
238+
ErrorReason.EXCEPTION,
239+
ErrorItem.FUNCTION,
240+
e.getMessage(),
241+
null,
242+
interpreter.getLineNumber(),
243+
interpreter.getPosition(),
244+
e
245+
)
246+
);
247+
} else {
248+
String originatingException = getRootCauseMessage(e);
249+
final String combinedMessage = String.format(
250+
"%s%nOriginating Exception:%n%s",
251+
e.getMessage(),
252+
originatingException
253+
);
254+
interpreter.addError(
255+
TemplateError.fromException(
256+
new TemplateSyntaxException(
257+
expression,
258+
(
259+
e.getCause() == null ||
260+
StringUtils.endsWith(originatingException, e.getCause().getMessage())
261+
)
262+
? e.getMessage()
263+
: combinedMessage,
264+
interpreter.getLineNumber(),
265+
e
266+
)
267+
)
268+
);
269+
}
270+
}
271+
272272
private void validateResult(Object result) {
273273
if (result instanceof NamedParameter) {
274274
throw new ELException(

0 commit comments

Comments
 (0)