69
69
* using view resolution (e.g., via {@code ContentNegotiatingViewResolver}),
70
70
* then {@code DefaultHandlerExceptionResolver} is good enough.
71
71
*
72
- * <p>Note that in order for an {@code @ControllerAdvice} sub-class to be
72
+ * <p>Note that in order for an {@code @ControllerAdvice} subclass to be
73
73
* detected, {@link ExceptionHandlerExceptionResolver} must be configured.
74
74
*
75
75
* @author Rossen Stoyanchev
@@ -121,7 +121,7 @@ public abstract class ResponseEntityExceptionHandler {
121
121
NoHandlerFoundException .class ,
122
122
AsyncRequestTimeoutException .class
123
123
})
124
- public final ResponseEntity <Object > handleException (Exception ex , WebRequest request ) {
124
+ public final ResponseEntity <Object > handleException (Exception ex , WebRequest request ) throws Exception {
125
125
HttpHeaders headers = new HttpHeaders ();
126
126
if (ex instanceof org .springframework .web .servlet .mvc .multiaction .NoSuchRequestHandlingMethodException ) {
127
127
HttpStatus status = HttpStatus .NOT_FOUND ;
@@ -185,38 +185,17 @@ else if (ex instanceof NoHandlerFoundException) {
185
185
}
186
186
else if (ex instanceof AsyncRequestTimeoutException ) {
187
187
HttpStatus status = HttpStatus .SERVICE_UNAVAILABLE ;
188
- return handleAsyncRequestTimeoutException (
189
- (AsyncRequestTimeoutException ) ex , headers , status , request );
188
+ return handleAsyncRequestTimeoutException ((AsyncRequestTimeoutException ) ex , headers , status , request );
190
189
}
191
190
else {
192
- if ( logger . isWarnEnabled ()) {
193
- logger . warn ( "Unknown exception type: " + ex . getClass (). getName ());
194
- }
195
- HttpStatus status = HttpStatus . INTERNAL_SERVER_ERROR ;
196
- return handleExceptionInternal ( ex , null , headers , status , request ) ;
191
+ // Unknown exception, typically a wrapper with a common MVC exception as cause
192
+ // (since @ExceptionHandler type declarations also match first-level causes):
193
+ // We only deal with top-level MVC exceptions here, so let's rethrow the given
194
+ // exception for further processing through the HandlerExceptionResolver chain.
195
+ throw ex ;
197
196
}
198
197
}
199
198
200
- /**
201
- * A single place to customize the response body of all Exception types.
202
- * <p>The default implementation sets the {@link WebUtils#ERROR_EXCEPTION_ATTRIBUTE}
203
- * request attribute and creates a {@link ResponseEntity} from the given
204
- * body, headers, and status.
205
- * @param ex the exception
206
- * @param body the body for the response
207
- * @param headers the headers for the response
208
- * @param status the response status
209
- * @param request the current request
210
- */
211
- protected ResponseEntity <Object > handleExceptionInternal (Exception ex , Object body ,
212
- HttpHeaders headers , HttpStatus status , WebRequest request ) {
213
-
214
- if (HttpStatus .INTERNAL_SERVER_ERROR .equals (status )) {
215
- request .setAttribute (WebUtils .ERROR_EXCEPTION_ATTRIBUTE , ex , WebRequest .SCOPE_REQUEST );
216
- }
217
- return new ResponseEntity <Object >(body , headers , status );
218
- }
219
-
220
199
/**
221
200
* Customize the response for NoSuchRequestHandlingMethodException.
222
201
* <p>This method logs a warning and delegates to {@link #handleExceptionInternal}.
@@ -228,7 +207,8 @@ protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object bo
228
207
* @deprecated as of 4.3, along with {@link org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException}
229
208
*/
230
209
@ Deprecated
231
- protected ResponseEntity <Object > handleNoSuchRequestHandlingMethod (org .springframework .web .servlet .mvc .multiaction .NoSuchRequestHandlingMethodException ex ,
210
+ protected ResponseEntity <Object > handleNoSuchRequestHandlingMethod (
211
+ org .springframework .web .servlet .mvc .multiaction .NoSuchRequestHandlingMethodException ex ,
232
212
HttpHeaders headers , HttpStatus status , WebRequest request ) {
233
213
234
214
pageNotFoundLogger .warn (ex .getMessage ());
@@ -246,8 +226,8 @@ protected ResponseEntity<Object> handleNoSuchRequestHandlingMethod(org.springfra
246
226
* @param request the current request
247
227
* @return a {@code ResponseEntity} instance
248
228
*/
249
- protected ResponseEntity <Object > handleHttpRequestMethodNotSupported (HttpRequestMethodNotSupportedException ex ,
250
- HttpHeaders headers , HttpStatus status , WebRequest request ) {
229
+ protected ResponseEntity <Object > handleHttpRequestMethodNotSupported (
230
+ HttpRequestMethodNotSupportedException ex , HttpHeaders headers , HttpStatus status , WebRequest request ) {
251
231
252
232
pageNotFoundLogger .warn (ex .getMessage ());
253
233
@@ -268,8 +248,8 @@ protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequest
268
248
* @param request the current request
269
249
* @return a {@code ResponseEntity} instance
270
250
*/
271
- protected ResponseEntity <Object > handleHttpMediaTypeNotSupported (HttpMediaTypeNotSupportedException ex ,
272
- HttpHeaders headers , HttpStatus status , WebRequest request ) {
251
+ protected ResponseEntity <Object > handleHttpMediaTypeNotSupported (
252
+ HttpMediaTypeNotSupportedException ex , HttpHeaders headers , HttpStatus status , WebRequest request ) {
273
253
274
254
List <MediaType > mediaTypes = ex .getSupportedMediaTypes ();
275
255
if (!CollectionUtils .isEmpty (mediaTypes )) {
@@ -288,8 +268,8 @@ protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNo
288
268
* @param request the current request
289
269
* @return a {@code ResponseEntity} instance
290
270
*/
291
- protected ResponseEntity <Object > handleHttpMediaTypeNotAcceptable (HttpMediaTypeNotAcceptableException ex ,
292
- HttpHeaders headers , HttpStatus status , WebRequest request ) {
271
+ protected ResponseEntity <Object > handleHttpMediaTypeNotAcceptable (
272
+ HttpMediaTypeNotAcceptableException ex , HttpHeaders headers , HttpStatus status , WebRequest request ) {
293
273
294
274
return handleExceptionInternal (ex , null , headers , status , request );
295
275
}
@@ -304,8 +284,8 @@ protected ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(HttpMediaTypeN
304
284
* @return a {@code ResponseEntity} instance
305
285
* @since 4.2
306
286
*/
307
- protected ResponseEntity <Object > handleMissingPathVariable (MissingPathVariableException ex ,
308
- HttpHeaders headers , HttpStatus status , WebRequest request ) {
287
+ protected ResponseEntity <Object > handleMissingPathVariable (
288
+ MissingPathVariableException ex , HttpHeaders headers , HttpStatus status , WebRequest request ) {
309
289
310
290
return handleExceptionInternal (ex , null , headers , status , request );
311
291
}
@@ -319,8 +299,8 @@ protected ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableEx
319
299
* @param request the current request
320
300
* @return a {@code ResponseEntity} instance
321
301
*/
322
- protected ResponseEntity <Object > handleMissingServletRequestParameter (MissingServletRequestParameterException ex ,
323
- HttpHeaders headers , HttpStatus status , WebRequest request ) {
302
+ protected ResponseEntity <Object > handleMissingServletRequestParameter (
303
+ MissingServletRequestParameterException ex , HttpHeaders headers , HttpStatus status , WebRequest request ) {
324
304
325
305
return handleExceptionInternal (ex , null , headers , status , request );
326
306
}
@@ -334,8 +314,8 @@ protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingSer
334
314
* @param request the current request
335
315
* @return a {@code ResponseEntity} instance
336
316
*/
337
- protected ResponseEntity <Object > handleServletRequestBindingException (ServletRequestBindingException ex ,
338
- HttpHeaders headers , HttpStatus status , WebRequest request ) {
317
+ protected ResponseEntity <Object > handleServletRequestBindingException (
318
+ ServletRequestBindingException ex , HttpHeaders headers , HttpStatus status , WebRequest request ) {
339
319
340
320
return handleExceptionInternal (ex , null , headers , status , request );
341
321
}
@@ -349,8 +329,8 @@ protected ResponseEntity<Object> handleServletRequestBindingException(ServletReq
349
329
* @param request the current request
350
330
* @return a {@code ResponseEntity} instance
351
331
*/
352
- protected ResponseEntity <Object > handleConversionNotSupported (ConversionNotSupportedException ex ,
353
- HttpHeaders headers , HttpStatus status , WebRequest request ) {
332
+ protected ResponseEntity <Object > handleConversionNotSupported (
333
+ ConversionNotSupportedException ex , HttpHeaders headers , HttpStatus status , WebRequest request ) {
354
334
355
335
return handleExceptionInternal (ex , null , headers , status , request );
356
336
}
@@ -364,8 +344,8 @@ protected ResponseEntity<Object> handleConversionNotSupported(ConversionNotSuppo
364
344
* @param request the current request
365
345
* @return a {@code ResponseEntity} instance
366
346
*/
367
- protected ResponseEntity <Object > handleTypeMismatch (TypeMismatchException ex , HttpHeaders headers ,
368
- HttpStatus status , WebRequest request ) {
347
+ protected ResponseEntity <Object > handleTypeMismatch (
348
+ TypeMismatchException ex , HttpHeaders headers , HttpStatus status , WebRequest request ) {
369
349
370
350
return handleExceptionInternal (ex , null , headers , status , request );
371
351
}
@@ -379,8 +359,8 @@ protected ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex, Ht
379
359
* @param request the current request
380
360
* @return a {@code ResponseEntity} instance
381
361
*/
382
- protected ResponseEntity <Object > handleHttpMessageNotReadable (HttpMessageNotReadableException ex ,
383
- HttpHeaders headers , HttpStatus status , WebRequest request ) {
362
+ protected ResponseEntity <Object > handleHttpMessageNotReadable (
363
+ HttpMessageNotReadableException ex , HttpHeaders headers , HttpStatus status , WebRequest request ) {
384
364
385
365
return handleExceptionInternal (ex , null , headers , status , request );
386
366
}
@@ -394,8 +374,8 @@ protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotRead
394
374
* @param request the current request
395
375
* @return a {@code ResponseEntity} instance
396
376
*/
397
- protected ResponseEntity <Object > handleHttpMessageNotWritable (HttpMessageNotWritableException ex ,
398
- HttpHeaders headers , HttpStatus status , WebRequest request ) {
377
+ protected ResponseEntity <Object > handleHttpMessageNotWritable (
378
+ HttpMessageNotWritableException ex , HttpHeaders headers , HttpStatus status , WebRequest request ) {
399
379
400
380
return handleExceptionInternal (ex , null , headers , status , request );
401
381
}
@@ -409,8 +389,8 @@ protected ResponseEntity<Object> handleHttpMessageNotWritable(HttpMessageNotWrit
409
389
* @param request the current request
410
390
* @return a {@code ResponseEntity} instance
411
391
*/
412
- protected ResponseEntity <Object > handleMethodArgumentNotValid (MethodArgumentNotValidException ex ,
413
- HttpHeaders headers , HttpStatus status , WebRequest request ) {
392
+ protected ResponseEntity <Object > handleMethodArgumentNotValid (
393
+ MethodArgumentNotValidException ex , HttpHeaders headers , HttpStatus status , WebRequest request ) {
414
394
415
395
return handleExceptionInternal (ex , null , headers , status , request );
416
396
}
@@ -424,8 +404,8 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotV
424
404
* @param request the current request
425
405
* @return a {@code ResponseEntity} instance
426
406
*/
427
- protected ResponseEntity <Object > handleMissingServletRequestPart (MissingServletRequestPartException ex ,
428
- HttpHeaders headers , HttpStatus status , WebRequest request ) {
407
+ protected ResponseEntity <Object > handleMissingServletRequestPart (
408
+ MissingServletRequestPartException ex , HttpHeaders headers , HttpStatus status , WebRequest request ) {
429
409
430
410
return handleExceptionInternal (ex , null , headers , status , request );
431
411
}
@@ -439,8 +419,8 @@ protected ResponseEntity<Object> handleMissingServletRequestPart(MissingServletR
439
419
* @param request the current request
440
420
* @return a {@code ResponseEntity} instance
441
421
*/
442
- protected ResponseEntity <Object > handleBindException (BindException ex , HttpHeaders headers ,
443
- HttpStatus status , WebRequest request ) {
422
+ protected ResponseEntity <Object > handleBindException (
423
+ BindException ex , HttpHeaders headers , HttpStatus status , WebRequest request ) {
444
424
445
425
return handleExceptionInternal (ex , null , headers , status , request );
446
426
}
@@ -489,4 +469,24 @@ protected ResponseEntity<Object> handleAsyncRequestTimeoutException(
489
469
return handleExceptionInternal (ex , null , headers , status , webRequest );
490
470
}
491
471
472
+ /**
473
+ * A single place to customize the response body of all Exception types.
474
+ * <p>The default implementation sets the {@link WebUtils#ERROR_EXCEPTION_ATTRIBUTE}
475
+ * request attribute and creates a {@link ResponseEntity} from the given
476
+ * body, headers, and status.
477
+ * @param ex the exception
478
+ * @param body the body for the response
479
+ * @param headers the headers for the response
480
+ * @param status the response status
481
+ * @param request the current request
482
+ */
483
+ protected ResponseEntity <Object > handleExceptionInternal (
484
+ Exception ex , Object body , HttpHeaders headers , HttpStatus status , WebRequest request ) {
485
+
486
+ if (HttpStatus .INTERNAL_SERVER_ERROR .equals (status )) {
487
+ request .setAttribute (WebUtils .ERROR_EXCEPTION_ATTRIBUTE , ex , WebRequest .SCOPE_REQUEST );
488
+ }
489
+ return new ResponseEntity <Object >(body , headers , status );
490
+ }
491
+
492
492
}
0 commit comments