Browse Source

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
pull/30300/merge
rstoyanchev 1 year ago
parent
commit
a53d3f3cea
  1. 21
      spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerAdapter.java
  2. 22
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java

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

@ -47,6 +47,7 @@ import org.springframework.web.reactive.HandlerMapping; @@ -47,6 +47,7 @@ import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.HandlerResult;
import org.springframework.web.reactive.result.method.InvocableHandlerMethod;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.DisconnectedClientHelper;
/**
* Supports the invocation of
@ -61,6 +62,16 @@ public class RequestMappingHandlerAdapter @@ -61,6 +62,16 @@ public class RequestMappingHandlerAdapter
private static final Log logger = LogFactory.getLog(RequestMappingHandlerAdapter.class);
/**
* Log category to use for network failure after a client has gone away.
* @see DisconnectedClientHelper
*/
private static final String DISCONNECTED_CLIENT_LOG_CATEGORY =
"org.springframework.web.reactive.result.method.annotation.DisconnectedClient";
private static final DisconnectedClientHelper disconnectedClientHelper =
new DisconnectedClientHelper(DISCONNECTED_CLIENT_LOG_CATEGORY);
private List<HttpMessageReader<?>> messageReaders = Collections.emptyList();
@ -298,10 +309,12 @@ public class RequestMappingHandlerAdapter @@ -298,10 +309,12 @@ public class RequestMappingHandlerAdapter
return invocable.invoke(exchange, bindingContext, arguments);
}
catch (Throwable invocationEx) {
// Any other than the original exception (or a cause) is unintended here,
// probably an accident (e.g. failed assertion or the like).
if (!exceptions.contains(invocationEx) && logger.isWarnEnabled()) {
logger.warn(exchange.getLogPrefix() + "Failure in @ExceptionHandler " + invocable, invocationEx);
if (!disconnectedClientHelper.checkAndLogClientDisconnectedException(invocationEx)) {
// Any other than the original exception (or a cause) is unintended here,
// probably an accident (e.g. failed assertion or the like).
if (!exceptions.contains(invocationEx) && logger.isWarnEnabled()) {
logger.warn(exchange.getLogPrefix() + "Failure in @ExceptionHandler " + invocable, invocationEx);
}
}
}
}

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

@ -58,6 +58,7 @@ import org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionRes @@ -58,6 +58,7 @@ import org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionRes
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import org.springframework.web.servlet.support.RequestContextUtils;
import org.springframework.web.util.DisconnectedClientHelper;
/**
* An {@link AbstractHandlerMethodExceptionResolver} that resolves exceptions
@ -76,6 +77,17 @@ import org.springframework.web.servlet.support.RequestContextUtils; @@ -76,6 +77,17 @@ import org.springframework.web.servlet.support.RequestContextUtils;
public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExceptionResolver
implements ApplicationContextAware, InitializingBean {
/**
* Log category to use for network failure after a client has gone away.
* @see DisconnectedClientHelper
*/
private static final String DISCONNECTED_CLIENT_LOG_CATEGORY =
"org.springframework.web.servlet.mvc.method.annotation.DisconnectedClient";
private static final DisconnectedClientHelper disconnectedClientHelper =
new DisconnectedClientHelper(DISCONNECTED_CLIENT_LOG_CATEGORY);
@Nullable
private List<HandlerMethodArgumentResolver> customArgumentResolvers;
@ -420,10 +432,12 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce @@ -420,10 +432,12 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, arguments);
}
catch (Throwable invocationEx) {
// Any other than the original exception (or a cause) is unintended here,
// probably an accident (e.g. failed assertion or the like).
if (!exceptions.contains(invocationEx) && logger.isWarnEnabled()) {
logger.warn("Failure in @ExceptionHandler " + exceptionHandlerMethod, invocationEx);
if (!disconnectedClientHelper.checkAndLogClientDisconnectedException(invocationEx)) {
// Any other than the original exception (or a cause) is unintended here,
// probably an accident (e.g. failed assertion or the like).
if (!exceptions.contains(invocationEx) && logger.isWarnEnabled()) {
logger.warn("Failure in @ExceptionHandler " + exceptionHandlerMethod, invocationEx);
}
}
// Continue with default processing of the original exception...
return null;

Loading…
Cancel
Save