|
13 | 13 | import com.hubspot.jinjava.interpret.InvalidArgumentException; |
14 | 14 | import com.hubspot.jinjava.interpret.InvalidInputException; |
15 | 15 | import com.hubspot.jinjava.interpret.JinjavaInterpreter; |
| 16 | +import com.hubspot.jinjava.interpret.OutputTooBigException; |
16 | 17 | import com.hubspot.jinjava.interpret.TemplateError; |
17 | 18 | import com.hubspot.jinjava.interpret.TemplateError.ErrorItem; |
18 | 19 | import com.hubspot.jinjava.interpret.TemplateError.ErrorReason; |
@@ -154,79 +155,7 @@ private Object resolveExpression(String expression, boolean addToResolvedExpress |
154 | 155 | ) |
155 | 156 | ); |
156 | 157 | } 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 | | - } |
| 158 | + handleELException(expression, e); |
230 | 159 | } catch (DisabledException e) { |
231 | 160 | interpreter.addError( |
232 | 161 | new TemplateError( |
@@ -269,6 +198,91 @@ private Object resolveExpression(String expression, boolean addToResolvedExpress |
269 | 198 | return null; |
270 | 199 | } |
271 | 200 |
|
| 201 | + private void handleELException(String expression, ELException e) { |
| 202 | + if (e.getCause() != null && e.getCause() instanceof DeferredValueException) { |
| 203 | + throw (DeferredValueException) e.getCause(); |
| 204 | + } |
| 205 | + if (e.getCause() != null && e.getCause() instanceof TemplateSyntaxException) { |
| 206 | + interpreter.addError( |
| 207 | + TemplateError.fromException((TemplateSyntaxException) e.getCause()) |
| 208 | + ); |
| 209 | + } else if (e.getCause() != null && e.getCause() instanceof InvalidInputException) { |
| 210 | + interpreter.addError( |
| 211 | + TemplateError.fromInvalidInputException((InvalidInputException) e.getCause()) |
| 212 | + ); |
| 213 | + } else if (e.getCause() != null && e.getCause() instanceof InvalidArgumentException) { |
| 214 | + interpreter.addError( |
| 215 | + TemplateError.fromInvalidArgumentException( |
| 216 | + (InvalidArgumentException) e.getCause() |
| 217 | + ) |
| 218 | + ); |
| 219 | + } else if ( |
| 220 | + e.getCause() != null && e.getCause() instanceof CollectionTooBigException |
| 221 | + ) { |
| 222 | + interpreter.addError( |
| 223 | + new TemplateError( |
| 224 | + ErrorType.FATAL, |
| 225 | + ErrorReason.COLLECTION_TOO_BIG, |
| 226 | + e.getCause().getMessage(), |
| 227 | + null, |
| 228 | + interpreter.getLineNumber(), |
| 229 | + interpreter.getPosition(), |
| 230 | + e |
| 231 | + ) |
| 232 | + ); |
| 233 | + // rethrow because this is a hard limit and it will likely only happen in loops that we need to terminate |
| 234 | + throw e; |
| 235 | + } else if (e.getCause() != null && e.getCause() instanceof IndexOutOfRangeException) { |
| 236 | + interpreter.addError( |
| 237 | + new TemplateError( |
| 238 | + ErrorType.WARNING, |
| 239 | + ErrorReason.EXCEPTION, |
| 240 | + ErrorItem.FUNCTION, |
| 241 | + e.getMessage(), |
| 242 | + null, |
| 243 | + interpreter.getLineNumber(), |
| 244 | + interpreter.getPosition(), |
| 245 | + e |
| 246 | + ) |
| 247 | + ); |
| 248 | + } else if (e.getCause() != null && e.getCause() instanceof OutputTooBigException) { |
| 249 | + interpreter.addError( |
| 250 | + new TemplateError( |
| 251 | + ErrorType.FATAL, |
| 252 | + ErrorReason.OUTPUT_TOO_BIG, |
| 253 | + ErrorItem.FUNCTION, |
| 254 | + e.getCause().getMessage(), |
| 255 | + null, |
| 256 | + interpreter.getLineNumber(), |
| 257 | + interpreter.getPosition(), |
| 258 | + e |
| 259 | + ) |
| 260 | + ); |
| 261 | + } else { |
| 262 | + String originatingException = getRootCauseMessage(e); |
| 263 | + final String combinedMessage = String.format( |
| 264 | + "%s%nOriginating Exception:%n%s", |
| 265 | + e.getMessage(), |
| 266 | + originatingException |
| 267 | + ); |
| 268 | + interpreter.addError( |
| 269 | + TemplateError.fromException( |
| 270 | + new TemplateSyntaxException( |
| 271 | + expression, |
| 272 | + ( |
| 273 | + e.getCause() == null || |
| 274 | + StringUtils.endsWith(originatingException, e.getCause().getMessage()) |
| 275 | + ) |
| 276 | + ? e.getMessage() |
| 277 | + : combinedMessage, |
| 278 | + interpreter.getLineNumber(), |
| 279 | + e |
| 280 | + ) |
| 281 | + ) |
| 282 | + ); |
| 283 | + } |
| 284 | + } |
| 285 | + |
272 | 286 | private void validateResult(Object result) { |
273 | 287 | if (result instanceof NamedParameter) { |
274 | 288 | throw new ELException( |
|
0 commit comments