Skip to content

Commit

Permalink
Support HandlerMethod arg in @ExceptionHandler methods
Browse files Browse the repository at this point in the history
Issue: SPR-12605
  • Loading branch information
rstoyanchev committed Jan 21, 2015
1 parent 2bf6b41 commit d68fde5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -359,7 +359,7 @@ protected ModelAndView doResolveHandlerMethodException(HttpServletRequest reques
if (logger.isDebugEnabled()) {
logger.debug("Invoking @ExceptionHandler method: " + exceptionHandlerMethod);
}
exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception);
exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, handlerMethod);
}
catch (Exception invocationEx) {
if (logger.isErrorEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -201,6 +201,23 @@ public void resolveExceptionGlobalHandlerOrdered() throws Exception {
assertEquals("TestExceptionResolver: IllegalStateException", this.response.getContentAsString());
}

// SPR-12605

@Test
public void resolveExceptionWithHandlerMethodArg() throws Exception {
AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);
this.resolver.setApplicationContext(cxt);
this.resolver.afterPropertiesSet();

ArrayIndexOutOfBoundsException ex = new ArrayIndexOutOfBoundsException();
HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle");
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ex);

assertNotNull("Exception was not handled", mav);
assertTrue(mav.isEmpty());
assertEquals("HandlerMethod: handle", this.response.getContentAsString());
}

@Test
public void resolveExceptionControllerAdviceHandler() throws Exception {
AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyControllerAdviceConfig.class);
Expand Down Expand Up @@ -289,6 +306,12 @@ static class TestExceptionResolver {
public String handleException(IllegalStateException ex) {
return "TestExceptionResolver: " + ClassUtils.getShortName(ex.getClass());
}

@ExceptionHandler(ArrayIndexOutOfBoundsException.class)
@ResponseBody
public String handleWithHandlerMethod(HandlerMethod handlerMethod) {
return "HandlerMethod: " + handlerMethod.getMethod().getName();
}
}

@ControllerAdvice
Expand Down

0 comments on commit d68fde5

Please sign in to comment.