Skip to content

Commit d68fde5

Browse files
committed
Support HandlerMethod arg in @ExceptionHandler methods
Issue: SPR-12605
1 parent 2bf6b41 commit d68fde5

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -359,7 +359,7 @@ protected ModelAndView doResolveHandlerMethodException(HttpServletRequest reques
359359
if (logger.isDebugEnabled()) {
360360
logger.debug("Invoking @ExceptionHandler method: " + exceptionHandlerMethod);
361361
}
362-
exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception);
362+
exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, handlerMethod);
363363
}
364364
catch (Exception invocationEx) {
365365
if (logger.isErrorEnabled()) {

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolverTests.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -201,6 +201,23 @@ public void resolveExceptionGlobalHandlerOrdered() throws Exception {
201201
assertEquals("TestExceptionResolver: IllegalStateException", this.response.getContentAsString());
202202
}
203203

204+
// SPR-12605
205+
206+
@Test
207+
public void resolveExceptionWithHandlerMethodArg() throws Exception {
208+
AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);
209+
this.resolver.setApplicationContext(cxt);
210+
this.resolver.afterPropertiesSet();
211+
212+
ArrayIndexOutOfBoundsException ex = new ArrayIndexOutOfBoundsException();
213+
HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle");
214+
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex);
215+
216+
assertNotNull("Exception was not handled", mav);
217+
assertTrue(mav.isEmpty());
218+
assertEquals("HandlerMethod: handle", this.response.getContentAsString());
219+
}
220+
204221
@Test
205222
public void resolveExceptionControllerAdviceHandler() throws Exception {
206223
AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyControllerAdviceConfig.class);
@@ -289,6 +306,12 @@ static class TestExceptionResolver {
289306
public String handleException(IllegalStateException ex) {
290307
return "TestExceptionResolver: " + ClassUtils.getShortName(ex.getClass());
291308
}
309+
310+
@ExceptionHandler(ArrayIndexOutOfBoundsException.class)
311+
@ResponseBody
312+
public String handleWithHandlerMethod(HandlerMethod handlerMethod) {
313+
return "HandlerMethod: " + handlerMethod.getMethod().getName();
314+
}
292315
}
293316

294317
@ControllerAdvice

0 commit comments

Comments
 (0)