Skip to content

Commit 5434edd

Browse files
committed
Avoid sendError call when response committed already (Tomcat 10.1.16)
Closes gh-32206 (cherry picked from commit 4ed3372)
1 parent 5f13ea9 commit 5434edd

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -464,8 +464,8 @@ protected ModelAndView handleErrorResponse(ErrorResponse errorResponse,
464464
response.sendError(status);
465465
}
466466
}
467-
else {
468-
logger.warn("Ignoring exception, response committed. : " + errorResponse);
467+
else if (logger.isWarnEnabled()) {
468+
logger.warn("Ignoring exception, response committed already: " + errorResponse);
469469
}
470470

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

527-
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
527+
if (!response.isCommitted()) {
528+
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
529+
}
530+
else if (logger.isWarnEnabled()) {
531+
logger.warn("Ignoring exception, response committed already: " + ex);
532+
}
528533
return new ModelAndView();
529534
}
530535

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

548-
sendServerError(ex, request, response);
553+
if (!response.isCommitted()) {
554+
sendServerError(ex, request, response);
555+
}
556+
else if (logger.isWarnEnabled()) {
557+
logger.warn("Ignoring exception, response committed already: " + ex);
558+
}
549559
return new ModelAndView();
550560
}
551561

0 commit comments

Comments
 (0)