Skip to content

Commit 4ed3372

Browse files
committed
Avoid sendError call when response committed already (Tomcat 10.1.16)
Closes gh-32206
1 parent 81cdfaf commit 4ed3372

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.
@@ -524,8 +524,8 @@ protected ModelAndView handleErrorResponse(ErrorResponse errorResponse,
524524
response.sendError(status);
525525
}
526526
}
527-
else {
528-
logger.warn("Ignoring exception, response committed. : " + errorResponse);
527+
else if (logger.isWarnEnabled()) {
528+
logger.warn("Ignoring exception, response committed already: " + errorResponse);
529529
}
530530

531531
return new ModelAndView();
@@ -584,14 +584,19 @@ protected ModelAndView handleTypeMismatch(TypeMismatchException ex,
584584
protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
585585
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
586586

587-
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
587+
if (!response.isCommitted()) {
588+
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
589+
}
590+
else if (logger.isWarnEnabled()) {
591+
logger.warn("Ignoring exception, response committed already: " + ex);
592+
}
588593
return new ModelAndView();
589594
}
590595

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

608-
sendServerError(ex, request, response);
613+
if (!response.isCommitted()) {
614+
sendServerError(ex, request, response);
615+
}
616+
else if (logger.isWarnEnabled()) {
617+
logger.warn("Ignoring exception, response committed already: " + ex);
618+
}
609619
return new ModelAndView();
610620
}
611621

0 commit comments

Comments
 (0)