Skip to content

Commit a53d3f3

Browse files
committed
Apply DisconnectedClientHelper to @ExceptionHandler methods
Use the helper to reduce logging when an @ExceptionHandler fails to write to the response due to a network failure where the client has gone away. Closes gh-26181
1 parent a8019f2 commit a53d3f3

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.web.reactive.HandlerResult;
4848
import org.springframework.web.reactive.result.method.InvocableHandlerMethod;
4949
import org.springframework.web.server.ServerWebExchange;
50+
import org.springframework.web.util.DisconnectedClientHelper;
5051

5152
/**
5253
* Supports the invocation of
@@ -61,6 +62,16 @@ public class RequestMappingHandlerAdapter
6162

6263
private static final Log logger = LogFactory.getLog(RequestMappingHandlerAdapter.class);
6364

65+
/**
66+
* Log category to use for network failure after a client has gone away.
67+
* @see DisconnectedClientHelper
68+
*/
69+
private static final String DISCONNECTED_CLIENT_LOG_CATEGORY =
70+
"org.springframework.web.reactive.result.method.annotation.DisconnectedClient";
71+
72+
private static final DisconnectedClientHelper disconnectedClientHelper =
73+
new DisconnectedClientHelper(DISCONNECTED_CLIENT_LOG_CATEGORY);
74+
6475

6576
private List<HttpMessageReader<?>> messageReaders = Collections.emptyList();
6677

@@ -298,10 +309,12 @@ private Mono<HandlerResult> handleException(
298309
return invocable.invoke(exchange, bindingContext, arguments);
299310
}
300311
catch (Throwable invocationEx) {
301-
// Any other than the original exception (or a cause) is unintended here,
302-
// probably an accident (e.g. failed assertion or the like).
303-
if (!exceptions.contains(invocationEx) && logger.isWarnEnabled()) {
304-
logger.warn(exchange.getLogPrefix() + "Failure in @ExceptionHandler " + invocable, invocationEx);
312+
if (!disconnectedClientHelper.checkAndLogClientDisconnectedException(invocationEx)) {
313+
// Any other than the original exception (or a cause) is unintended here,
314+
// probably an accident (e.g. failed assertion or the like).
315+
if (!exceptions.contains(invocationEx) && logger.isWarnEnabled()) {
316+
logger.warn(exchange.getLogPrefix() + "Failure in @ExceptionHandler " + invocable, invocationEx);
317+
}
305318
}
306319
}
307320
}

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
5959
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
6060
import org.springframework.web.servlet.support.RequestContextUtils;
61+
import org.springframework.web.util.DisconnectedClientHelper;
6162

6263
/**
6364
* An {@link AbstractHandlerMethodExceptionResolver} that resolves exceptions
@@ -76,6 +77,17 @@
7677
public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExceptionResolver
7778
implements ApplicationContextAware, InitializingBean {
7879

80+
/**
81+
* Log category to use for network failure after a client has gone away.
82+
* @see DisconnectedClientHelper
83+
*/
84+
private static final String DISCONNECTED_CLIENT_LOG_CATEGORY =
85+
"org.springframework.web.servlet.mvc.method.annotation.DisconnectedClient";
86+
87+
private static final DisconnectedClientHelper disconnectedClientHelper =
88+
new DisconnectedClientHelper(DISCONNECTED_CLIENT_LOG_CATEGORY);
89+
90+
7991
@Nullable
8092
private List<HandlerMethodArgumentResolver> customArgumentResolvers;
8193

@@ -420,10 +432,12 @@ protected ModelAndView doResolveHandlerMethodException(HttpServletRequest reques
420432
exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, arguments);
421433
}
422434
catch (Throwable invocationEx) {
423-
// Any other than the original exception (or a cause) is unintended here,
424-
// probably an accident (e.g. failed assertion or the like).
425-
if (!exceptions.contains(invocationEx) && logger.isWarnEnabled()) {
426-
logger.warn("Failure in @ExceptionHandler " + exceptionHandlerMethod, invocationEx);
435+
if (!disconnectedClientHelper.checkAndLogClientDisconnectedException(invocationEx)) {
436+
// Any other than the original exception (or a cause) is unintended here,
437+
// probably an accident (e.g. failed assertion or the like).
438+
if (!exceptions.contains(invocationEx) && logger.isWarnEnabled()) {
439+
logger.warn("Failure in @ExceptionHandler " + exceptionHandlerMethod, invocationEx);
440+
}
427441
}
428442
// Continue with default processing of the original exception...
429443
return null;

0 commit comments

Comments
 (0)