Skip to content

Commit 9e62c8e

Browse files
committed
DefaultHandlerExceptionResolver logs warn entries for conversion exceptions
Issue: SPR-13267
1 parent f0ac278 commit 9e62c8e

File tree

1 file changed

+48
-25
lines changed

1 file changed

+48
-25
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@
5656
* HandlerExceptionResolver} interface that resolves standard Spring exceptions and translates
5757
* them to corresponding HTTP status codes.
5858
*
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}.
6061
*
6162
* @author Arjen Poutsma
6263
* @author Rossen Stoyanchev
64+
* @author Juergen Hoeller
6365
* @since 3.0
64-
*
6566
* @see org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
6667
* @see #handleNoSuchRequestHandlingMethod
6768
* @see #handleHttpRequestMethodNotSupported
@@ -145,10 +146,12 @@ else if (ex instanceof HttpMessageNotWritableException) {
145146
return handleHttpMessageNotWritable((HttpMessageNotWritableException) ex, request, response, handler);
146147
}
147148
else if (ex instanceof MethodArgumentNotValidException) {
148-
return handleMethodArgumentNotValidException((MethodArgumentNotValidException) ex, request, response, handler);
149+
return handleMethodArgumentNotValidException((MethodArgumentNotValidException) ex, request, response,
150+
handler);
149151
}
150152
else if (ex instanceof MissingServletRequestPartException) {
151-
return handleMissingServletRequestPartException((MissingServletRequestPartException) ex, request, response, handler);
153+
return handleMissingServletRequestPartException((MissingServletRequestPartException) ex, request,
154+
response, handler);
152155
}
153156
else if (ex instanceof BindException) {
154157
return handleBindException((BindException) ex, request, response, handler);
@@ -158,7 +161,9 @@ else if (ex instanceof NoHandlerFoundException) {
158161
}
159162
}
160163
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+
}
162167
}
163168
return null;
164169
}
@@ -211,9 +216,10 @@ protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotS
211216

212217
/**
213218
* 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.
217223
* @param ex the HttpMediaTypeNotSupportedException to be handled
218224
* @param request current HTTP request
219225
* @param response current HTTP response
@@ -305,7 +311,7 @@ protected ModelAndView handleMissingServletRequestParameter(MissingServletReques
305311
protected ModelAndView handleServletRequestBindingException(ServletRequestBindingException ex,
306312
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
307313

308-
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
314+
response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage());
309315
return new ModelAndView();
310316
}
311317

@@ -323,21 +329,13 @@ protected ModelAndView handleServletRequestBindingException(ServletRequestBindin
323329
protected ModelAndView handleConversionNotSupported(ConversionNotSupportedException ex,
324330
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
325331

332+
if (logger.isWarnEnabled()) {
333+
logger.warn("Failed to convert request element: " + ex);
334+
}
326335
sendServerError(ex, request, response);
327336
return new ModelAndView();
328337
}
329338

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-
341339
/**
342340
* Handle the case when a {@link org.springframework.web.bind.WebDataBinder} conversion error occurs.
343341
* <p>The default implementation sends an HTTP 400 error, and returns an empty {@code ModelAndView}.
@@ -352,6 +350,9 @@ protected void sendServerError(Exception ex,
352350
protected ModelAndView handleTypeMismatch(TypeMismatchException ex,
353351
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
354352

353+
if (logger.isWarnEnabled()) {
354+
logger.warn("Failed to bind request element: " + ex);
355+
}
355356
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
356357
return new ModelAndView();
357358
}
@@ -372,6 +373,9 @@ protected ModelAndView handleTypeMismatch(TypeMismatchException ex,
372373
protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
373374
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
374375

376+
if (logger.isWarnEnabled()) {
377+
logger.warn("Failed to read HTTP message: " + ex);
378+
}
375379
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
376380
return new ModelAndView();
377381
}
@@ -392,6 +396,9 @@ protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableExcept
392396
protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableException ex,
393397
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
394398

399+
if (logger.isWarnEnabled()) {
400+
logger.warn("Failed to write HTTP message: " + ex);
401+
}
395402
sendServerError(ex, request, response);
396403
return new ModelAndView();
397404
}
@@ -408,6 +415,7 @@ protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableExcept
408415
*/
409416
protected ModelAndView handleMethodArgumentNotValidException(MethodArgumentNotValidException ex,
410417
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
418+
411419
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
412420
return new ModelAndView();
413421
}
@@ -424,6 +432,7 @@ protected ModelAndView handleMethodArgumentNotValidException(MethodArgumentNotVa
424432
*/
425433
protected ModelAndView handleMissingServletRequestPartException(MissingServletRequestPartException ex,
426434
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
435+
427436
response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage());
428437
return new ModelAndView();
429438
}
@@ -432,7 +441,7 @@ protected ModelAndView handleMissingServletRequestPartException(MissingServletRe
432441
* Handle the case where an {@linkplain ModelAttribute @ModelAttribute} method
433442
* argument has binding or validation errors and is not followed by another
434443
* 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.
436445
* @param request current HTTP request
437446
* @param response current HTTP response
438447
* @param handler the executed handler
@@ -441,14 +450,15 @@ protected ModelAndView handleMissingServletRequestPartException(MissingServletRe
441450
*/
442451
protected ModelAndView handleBindException(BindException ex, HttpServletRequest request,
443452
HttpServletResponse response, Object handler) throws IOException {
453+
444454
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
445455
return new ModelAndView();
446456
}
447457

448458
/**
449459
* 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,
452462
* or the NoHandlerFoundException could be rethrown as-is.
453463
* @param ex the NoHandlerFoundException to be handled
454464
* @param request current HTTP request
@@ -459,10 +469,23 @@ protected ModelAndView handleBindException(BindException ex, HttpServletRequest
459469
* @throws IOException potentially thrown from response.sendError()
460470
* @since 4.0
461471
*/
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+
464475
response.sendError(HttpServletResponse.SC_NOT_FOUND);
465476
return new ModelAndView();
466477
}
467478

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+
468491
}

0 commit comments

Comments
 (0)