Skip to content

Commit

Permalink
Avoid sendError call when response committed already (Tomcat 10.1.16)
Browse files Browse the repository at this point in the history
Closes gh-32206

(cherry picked from commit 4ed3372)
  • Loading branch information
jhoeller committed Feb 6, 2024
1 parent 5f13ea9 commit 5434edd
Showing 1 changed file with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -464,8 +464,8 @@ protected ModelAndView handleErrorResponse(ErrorResponse errorResponse,
response.sendError(status);
}
}
else {
logger.warn("Ignoring exception, response committed. : " + errorResponse);
else if (logger.isWarnEnabled()) {
logger.warn("Ignoring exception, response committed already: " + errorResponse);
}

return new ModelAndView();
Expand Down Expand Up @@ -524,14 +524,19 @@ protected ModelAndView handleTypeMismatch(TypeMismatchException ex,
protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {

response.sendError(HttpServletResponse.SC_BAD_REQUEST);
if (!response.isCommitted()) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
else if (logger.isWarnEnabled()) {
logger.warn("Ignoring exception, response committed already: " + ex);
}
return new ModelAndView();
}

/**
* Handle the case where a
* {@linkplain org.springframework.http.converter.HttpMessageConverter message converter}
* cannot write to an HTTP request.
* cannot write to an HTTP response.
* <p>The default implementation sends an HTTP 500 error, and returns an empty {@code ModelAndView}.
* Alternatively, a fallback view could be chosen, or the HttpMessageNotWritableException could
* be rethrown as-is.
Expand All @@ -545,7 +550,12 @@ protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableExcept
protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableException ex,
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {

sendServerError(ex, request, response);
if (!response.isCommitted()) {
sendServerError(ex, request, response);
}
else if (logger.isWarnEnabled()) {
logger.warn("Ignoring exception, response committed already: " + ex);
}
return new ModelAndView();
}

Expand Down

0 comments on commit 5434edd

Please sign in to comment.