Skip to content

Commit 7627c38

Browse files
committed
Exceptions thrown from @ExceptionHandler methods logged at warn level (instead of debug)
Issue: SPR-14861
1 parent 9ccffb6 commit 7627c38

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
5757

5858
private static final Log logger = LogFactory.getLog(RequestMappingHandlerAdapter.class);
5959

60-
6160
private final List<HttpMessageReader<?>> messageReaders = new ArrayList<>(10);
6261

6362
private WebBindingInitializer webBindingInitializer;
@@ -74,7 +73,6 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
7473
new ConcurrentHashMap<>(64);
7574

7675

77-
7876
public RequestMappingHandlerAdapter() {
7977
this.messageReaders.add(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
8078
this.messageReaders.add(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
@@ -226,28 +224,27 @@ public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {
226224
private Mono<HandlerResult> handleException(Throwable ex, HandlerMethod handlerMethod,
227225
BindingContext bindingContext, ServerWebExchange exchange) {
228226

229-
if (ex instanceof Exception) {
230-
InvocableHandlerMethod invocable = findExceptionHandler(handlerMethod, (Exception) ex);
231-
if (invocable != null) {
232-
try {
233-
if (logger.isDebugEnabled()) {
234-
logger.debug("Invoking @ExceptionHandler method: " + invocable);
235-
}
236-
invocable.setHandlerMethodArgumentResolvers(getArgumentResolvers());
237-
bindingContext.getModel().clear();
238-
return invocable.invokeForRequest(exchange, bindingContext, ex);
227+
InvocableHandlerMethod invocable = findExceptionHandler(handlerMethod, ex);
228+
if (invocable != null) {
229+
try {
230+
if (logger.isDebugEnabled()) {
231+
logger.debug("Invoking @ExceptionHandler method: " + invocable.getMethod());
239232
}
240-
catch (Exception invocationEx) {
241-
if (logger.isErrorEnabled()) {
242-
logger.error("Failed to invoke @ExceptionHandler method: " + invocable, invocationEx);
243-
}
233+
invocable.setHandlerMethodArgumentResolvers(getArgumentResolvers());
234+
bindingContext.getModel().clear();
235+
return invocable.invokeForRequest(exchange, bindingContext, ex);
236+
}
237+
catch (Throwable invocationEx) {
238+
if (logger.isWarnEnabled()) {
239+
logger.warn("Failed to invoke @ExceptionHandler method: " + invocable.getMethod(),
240+
invocationEx);
244241
}
245242
}
246243
}
247244
return Mono.error(ex);
248245
}
249246

250-
protected InvocableHandlerMethod findExceptionHandler(HandlerMethod handlerMethod, Exception exception) {
247+
protected InvocableHandlerMethod findExceptionHandler(HandlerMethod handlerMethod, Throwable exception) {
251248
if (handlerMethod == null) {
252249
return null;
253250
}
@@ -257,8 +254,8 @@ protected InvocableHandlerMethod findExceptionHandler(HandlerMethod handlerMetho
257254
resolver = new ExceptionHandlerMethodResolver(handlerType);
258255
this.exceptionHandlerCache.put(handlerType, resolver);
259256
}
260-
Method method = resolver.resolveMethod(exception);
257+
Method method = resolver.resolveMethodByExceptionType(exception.getClass());
261258
return (method != null ? new InvocableHandlerMethod(handlerMethod.getBean(), method) : null);
262259
}
263260

264-
}
261+
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.concurrent.ConcurrentHashMap;
2727
import javax.servlet.http.HttpServletRequest;
2828
import javax.servlet.http.HttpServletResponse;
29-
import javax.xml.transform.Source;
3029

3130
import org.springframework.beans.factory.InitializingBean;
3231
import org.springframework.context.ApplicationContext;
@@ -381,9 +380,9 @@ protected ModelAndView doResolveHandlerMethodException(HttpServletRequest reques
381380
exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, handlerMethod);
382381
}
383382
}
384-
catch (Exception invocationEx) {
385-
if (logger.isDebugEnabled()) {
386-
logger.debug("Failed to invoke @ExceptionHandler method: " + exceptionHandlerMethod, invocationEx);
383+
catch (Throwable invocationEx) {
384+
if (logger.isWarnEnabled()) {
385+
logger.warn("Failed to invoke @ExceptionHandler method: " + exceptionHandlerMethod, invocationEx);
387386
}
388387
return null;
389388
}

0 commit comments

Comments
 (0)