DefaultHandlerExceptionResolver logs warn entries for conversion exceptions

Issue: SPR-13267
This commit is contained in:
Juergen Hoeller
2015-07-28 12:14:42 +02:00
parent f0ac2784a4
commit 9e62c8efa1

View File

@@ -56,12 +56,13 @@ import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMeth
* HandlerExceptionResolver} interface that resolves standard Spring exceptions and translates
* them to corresponding HTTP status codes.
*
* <p>This exception resolver is enabled by default in the {@link org.springframework.web.servlet.DispatcherServlet}.
* <p>This exception resolver is enabled by default in the common Spring
* {@link org.springframework.web.servlet.DispatcherServlet}.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 3.0
*
* @see org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
* @see #handleNoSuchRequestHandlingMethod
* @see #handleHttpRequestMethodNotSupported
@@ -145,10 +146,12 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
return handleHttpMessageNotWritable((HttpMessageNotWritableException) ex, request, response, handler);
}
else if (ex instanceof MethodArgumentNotValidException) {
return handleMethodArgumentNotValidException((MethodArgumentNotValidException) ex, request, response, handler);
return handleMethodArgumentNotValidException((MethodArgumentNotValidException) ex, request, response,
handler);
}
else if (ex instanceof MissingServletRequestPartException) {
return handleMissingServletRequestPartException((MissingServletRequestPartException) ex, request, response, handler);
return handleMissingServletRequestPartException((MissingServletRequestPartException) ex, request,
response, handler);
}
else if (ex instanceof BindException) {
return handleBindException((BindException) ex, request, response, handler);
@@ -158,7 +161,9 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
}
}
catch (Exception handlerException) {
logger.warn("Handling of [" + ex.getClass().getName() + "] resulted in Exception", handlerException);
if (logger.isWarnEnabled()) {
logger.warn("Handling of [" + ex.getClass().getName() + "] resulted in Exception", handlerException);
}
}
return null;
}
@@ -211,9 +216,10 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
/**
* Handle the case where no {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}
* were found for the PUT or POSTed content. <p>The default implementation sends an HTTP 415 error,
* sets the "Accept" header, and returns an empty {@code ModelAndView}. Alternatively, a fallback
* view could be chosen, or the HttpMediaTypeNotSupportedException could be rethrown as-is.
* were found for the PUT or POSTed content.
* <p>The default implementation sends an HTTP 415 error, sets the "Accept" header,
* and returns an empty {@code ModelAndView}. Alternatively, a fallback view could
* be chosen, or the HttpMediaTypeNotSupportedException could be rethrown as-is.
* @param ex the HttpMediaTypeNotSupportedException to be handled
* @param request current HTTP request
* @param response current HTTP response
@@ -305,7 +311,7 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
protected ModelAndView handleServletRequestBindingException(ServletRequestBindingException ex,
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage());
return new ModelAndView();
}
@@ -323,21 +329,13 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
protected ModelAndView handleConversionNotSupported(ConversionNotSupportedException ex,
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
if (logger.isWarnEnabled()) {
logger.warn("Failed to convert request element: " + ex);
}
sendServerError(ex, request, response);
return new ModelAndView();
}
/**
* Invoked to send a server error. Sets the status to 500 and also sets the
* request attribute "javax.servlet.error.exception" to the Exception.
*/
protected void sendServerError(Exception ex,
HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setAttribute("javax.servlet.error.exception", ex);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
/**
* Handle the case when a {@link org.springframework.web.bind.WebDataBinder} conversion error occurs.
* <p>The default implementation sends an HTTP 400 error, and returns an empty {@code ModelAndView}.
@@ -352,6 +350,9 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
protected ModelAndView handleTypeMismatch(TypeMismatchException ex,
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
if (logger.isWarnEnabled()) {
logger.warn("Failed to bind request element: " + ex);
}
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return new ModelAndView();
}
@@ -372,6 +373,9 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
if (logger.isWarnEnabled()) {
logger.warn("Failed to read HTTP message: " + ex);
}
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return new ModelAndView();
}
@@ -392,6 +396,9 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableException ex,
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
if (logger.isWarnEnabled()) {
logger.warn("Failed to write HTTP message: " + ex);
}
sendServerError(ex, request, response);
return new ModelAndView();
}
@@ -408,6 +415,7 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
*/
protected ModelAndView handleMethodArgumentNotValidException(MethodArgumentNotValidException ex,
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return new ModelAndView();
}
@@ -424,6 +432,7 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
*/
protected ModelAndView handleMissingServletRequestPartException(MissingServletRequestPartException ex,
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage());
return new ModelAndView();
}
@@ -432,7 +441,7 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
* Handle the case where an {@linkplain ModelAttribute @ModelAttribute} method
* argument has binding or validation errors and is not followed by another
* method argument of type {@link BindingResult}.
* By default an HTTP 400 error is sent back to the client.
* By default, an HTTP 400 error is sent back to the client.
* @param request current HTTP request
* @param response current HTTP response
* @param handler the executed handler
@@ -441,14 +450,15 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
*/
protected ModelAndView handleBindException(BindException ex, HttpServletRequest request,
HttpServletResponse response, Object handler) throws IOException {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return new ModelAndView();
}
/**
* Handle the case where no handler was found during the dispatch.
* <p>The default sends an HTTP 404 error, and returns
* an empty {@code ModelAndView}. Alternatively, a fallback view could be chosen,
* <p>The default implementation sends an HTTP 404 error and returns an empty
* {@code ModelAndView}. Alternatively, a fallback view could be chosen,
* or the NoHandlerFoundException could be rethrown as-is.
* @param ex the NoHandlerFoundException to be handled
* @param request current HTTP request
@@ -459,10 +469,23 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
* @throws IOException potentially thrown from response.sendError()
* @since 4.0
*/
protected ModelAndView handleNoHandlerFoundException(NoHandlerFoundException ex, HttpServletRequest request,
HttpServletResponse response, Object handler) throws IOException {
protected ModelAndView handleNoHandlerFoundException(NoHandlerFoundException ex,
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return new ModelAndView();
}
/**
* Invoked to send a server error. Sets the status to 500 and also sets the
* request attribute "javax.servlet.error.exception" to the Exception.
*/
protected void sendServerError(Exception ex, HttpServletRequest request, HttpServletResponse response)
throws IOException {
request.setAttribute("javax.servlet.error.exception", ex);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}