56
56
* HandlerExceptionResolver} interface that resolves standard Spring exceptions and translates
57
57
* them to corresponding HTTP status codes.
58
58
*
59
- * <p>This exception resolver is enabled by default in the {@link org.springframework.web.servlet.DispatcherServlet}.
59
+ * <p>This exception resolver is enabled by default in the common Spring
60
+ * {@link org.springframework.web.servlet.DispatcherServlet}.
60
61
*
61
62
* @author Arjen Poutsma
62
63
* @author Rossen Stoyanchev
64
+ * @author Juergen Hoeller
63
65
* @since 3.0
64
- *
65
66
* @see org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
66
67
* @see #handleNoSuchRequestHandlingMethod
67
68
* @see #handleHttpRequestMethodNotSupported
@@ -145,10 +146,12 @@ else if (ex instanceof HttpMessageNotWritableException) {
145
146
return handleHttpMessageNotWritable ((HttpMessageNotWritableException ) ex , request , response , handler );
146
147
}
147
148
else if (ex instanceof MethodArgumentNotValidException ) {
148
- return handleMethodArgumentNotValidException ((MethodArgumentNotValidException ) ex , request , response , handler );
149
+ return handleMethodArgumentNotValidException ((MethodArgumentNotValidException ) ex , request , response ,
150
+ handler );
149
151
}
150
152
else if (ex instanceof MissingServletRequestPartException ) {
151
- return handleMissingServletRequestPartException ((MissingServletRequestPartException ) ex , request , response , handler );
153
+ return handleMissingServletRequestPartException ((MissingServletRequestPartException ) ex , request ,
154
+ response , handler );
152
155
}
153
156
else if (ex instanceof BindException ) {
154
157
return handleBindException ((BindException ) ex , request , response , handler );
@@ -158,7 +161,9 @@ else if (ex instanceof NoHandlerFoundException) {
158
161
}
159
162
}
160
163
catch (Exception handlerException ) {
161
- logger .warn ("Handling of [" + ex .getClass ().getName () + "] resulted in Exception" , handlerException );
164
+ if (logger .isWarnEnabled ()) {
165
+ logger .warn ("Handling of [" + ex .getClass ().getName () + "] resulted in Exception" , handlerException );
166
+ }
162
167
}
163
168
return null ;
164
169
}
@@ -211,9 +216,10 @@ protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotS
211
216
212
217
/**
213
218
* Handle the case where no {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}
214
- * were found for the PUT or POSTed content. <p>The default implementation sends an HTTP 415 error,
215
- * sets the "Accept" header, and returns an empty {@code ModelAndView}. Alternatively, a fallback
216
- * view could be chosen, or the HttpMediaTypeNotSupportedException could be rethrown as-is.
219
+ * were found for the PUT or POSTed content.
220
+ * <p>The default implementation sends an HTTP 415 error, sets the "Accept" header,
221
+ * and returns an empty {@code ModelAndView}. Alternatively, a fallback view could
222
+ * be chosen, or the HttpMediaTypeNotSupportedException could be rethrown as-is.
217
223
* @param ex the HttpMediaTypeNotSupportedException to be handled
218
224
* @param request current HTTP request
219
225
* @param response current HTTP response
@@ -305,7 +311,7 @@ protected ModelAndView handleMissingServletRequestParameter(MissingServletReques
305
311
protected ModelAndView handleServletRequestBindingException (ServletRequestBindingException ex ,
306
312
HttpServletRequest request , HttpServletResponse response , Object handler ) throws IOException {
307
313
308
- response .sendError (HttpServletResponse .SC_BAD_REQUEST );
314
+ response .sendError (HttpServletResponse .SC_BAD_REQUEST , ex . getMessage () );
309
315
return new ModelAndView ();
310
316
}
311
317
@@ -323,21 +329,13 @@ protected ModelAndView handleServletRequestBindingException(ServletRequestBindin
323
329
protected ModelAndView handleConversionNotSupported (ConversionNotSupportedException ex ,
324
330
HttpServletRequest request , HttpServletResponse response , Object handler ) throws IOException {
325
331
332
+ if (logger .isWarnEnabled ()) {
333
+ logger .warn ("Failed to convert request element: " + ex );
334
+ }
326
335
sendServerError (ex , request , response );
327
336
return new ModelAndView ();
328
337
}
329
338
330
- /**
331
- * Invoked to send a server error. Sets the status to 500 and also sets the
332
- * request attribute "javax.servlet.error.exception" to the Exception.
333
- */
334
- protected void sendServerError (Exception ex ,
335
- HttpServletRequest request , HttpServletResponse response ) throws IOException {
336
-
337
- request .setAttribute ("javax.servlet.error.exception" , ex );
338
- response .sendError (HttpServletResponse .SC_INTERNAL_SERVER_ERROR );
339
- }
340
-
341
339
/**
342
340
* Handle the case when a {@link org.springframework.web.bind.WebDataBinder} conversion error occurs.
343
341
* <p>The default implementation sends an HTTP 400 error, and returns an empty {@code ModelAndView}.
@@ -352,6 +350,9 @@ protected void sendServerError(Exception ex,
352
350
protected ModelAndView handleTypeMismatch (TypeMismatchException ex ,
353
351
HttpServletRequest request , HttpServletResponse response , Object handler ) throws IOException {
354
352
353
+ if (logger .isWarnEnabled ()) {
354
+ logger .warn ("Failed to bind request element: " + ex );
355
+ }
355
356
response .sendError (HttpServletResponse .SC_BAD_REQUEST );
356
357
return new ModelAndView ();
357
358
}
@@ -372,6 +373,9 @@ protected ModelAndView handleTypeMismatch(TypeMismatchException ex,
372
373
protected ModelAndView handleHttpMessageNotReadable (HttpMessageNotReadableException ex ,
373
374
HttpServletRequest request , HttpServletResponse response , Object handler ) throws IOException {
374
375
376
+ if (logger .isWarnEnabled ()) {
377
+ logger .warn ("Failed to read HTTP message: " + ex );
378
+ }
375
379
response .sendError (HttpServletResponse .SC_BAD_REQUEST );
376
380
return new ModelAndView ();
377
381
}
@@ -392,6 +396,9 @@ protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableExcept
392
396
protected ModelAndView handleHttpMessageNotWritable (HttpMessageNotWritableException ex ,
393
397
HttpServletRequest request , HttpServletResponse response , Object handler ) throws IOException {
394
398
399
+ if (logger .isWarnEnabled ()) {
400
+ logger .warn ("Failed to write HTTP message: " + ex );
401
+ }
395
402
sendServerError (ex , request , response );
396
403
return new ModelAndView ();
397
404
}
@@ -408,6 +415,7 @@ protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableExcept
408
415
*/
409
416
protected ModelAndView handleMethodArgumentNotValidException (MethodArgumentNotValidException ex ,
410
417
HttpServletRequest request , HttpServletResponse response , Object handler ) throws IOException {
418
+
411
419
response .sendError (HttpServletResponse .SC_BAD_REQUEST );
412
420
return new ModelAndView ();
413
421
}
@@ -424,6 +432,7 @@ protected ModelAndView handleMethodArgumentNotValidException(MethodArgumentNotVa
424
432
*/
425
433
protected ModelAndView handleMissingServletRequestPartException (MissingServletRequestPartException ex ,
426
434
HttpServletRequest request , HttpServletResponse response , Object handler ) throws IOException {
435
+
427
436
response .sendError (HttpServletResponse .SC_BAD_REQUEST , ex .getMessage ());
428
437
return new ModelAndView ();
429
438
}
@@ -432,7 +441,7 @@ protected ModelAndView handleMissingServletRequestPartException(MissingServletRe
432
441
* Handle the case where an {@linkplain ModelAttribute @ModelAttribute} method
433
442
* argument has binding or validation errors and is not followed by another
434
443
* method argument of type {@link BindingResult}.
435
- * By default an HTTP 400 error is sent back to the client.
444
+ * By default, an HTTP 400 error is sent back to the client.
436
445
* @param request current HTTP request
437
446
* @param response current HTTP response
438
447
* @param handler the executed handler
@@ -441,14 +450,15 @@ protected ModelAndView handleMissingServletRequestPartException(MissingServletRe
441
450
*/
442
451
protected ModelAndView handleBindException (BindException ex , HttpServletRequest request ,
443
452
HttpServletResponse response , Object handler ) throws IOException {
453
+
444
454
response .sendError (HttpServletResponse .SC_BAD_REQUEST );
445
455
return new ModelAndView ();
446
456
}
447
457
448
458
/**
449
459
* Handle the case where no handler was found during the dispatch.
450
- * <p>The default sends an HTTP 404 error, and returns
451
- * an empty {@code ModelAndView}. Alternatively, a fallback view could be chosen,
460
+ * <p>The default implementation sends an HTTP 404 error and returns an empty
461
+ * {@code ModelAndView}. Alternatively, a fallback view could be chosen,
452
462
* or the NoHandlerFoundException could be rethrown as-is.
453
463
* @param ex the NoHandlerFoundException to be handled
454
464
* @param request current HTTP request
@@ -459,10 +469,23 @@ protected ModelAndView handleBindException(BindException ex, HttpServletRequest
459
469
* @throws IOException potentially thrown from response.sendError()
460
470
* @since 4.0
461
471
*/
462
- protected ModelAndView handleNoHandlerFoundException (NoHandlerFoundException ex , HttpServletRequest request ,
463
- HttpServletResponse response , Object handler ) throws IOException {
472
+ protected ModelAndView handleNoHandlerFoundException (NoHandlerFoundException ex ,
473
+ HttpServletRequest request , HttpServletResponse response , Object handler ) throws IOException {
474
+
464
475
response .sendError (HttpServletResponse .SC_NOT_FOUND );
465
476
return new ModelAndView ();
466
477
}
467
478
479
+
480
+ /**
481
+ * Invoked to send a server error. Sets the status to 500 and also sets the
482
+ * request attribute "javax.servlet.error.exception" to the Exception.
483
+ */
484
+ protected void sendServerError (Exception ex , HttpServletRequest request , HttpServletResponse response )
485
+ throws IOException {
486
+
487
+ request .setAttribute ("javax.servlet.error.exception" , ex );
488
+ response .sendError (HttpServletResponse .SC_INTERNAL_SERVER_ERROR );
489
+ }
490
+
468
491
}
0 commit comments