Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions thrown from an ActionRequest are not handled via @ExceptionHandler [SPR-6959] #11624

Closed
spring-projects-issues opened this issue Mar 8, 2010 · 3 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Peter Oxenham opened SPR-6959 and commented

Exceptions thrown from a controller due to an ActionRequest are not handled via the AnnotationMethodHandlerExceptionResolver.

This seems to be because the 'handler' argument is never passed to the resolveException method.

I changed org.springframework.web.portlet.DispatcherPortlet.doRenderService(RenderRequest, RenderResponse) to call getHandler()
before the processing of the ACTION_EXCEPTION_RENDER_PARAMETER and this seemed to fix the problem (though I'm not sure of any other ramifications of this change).


Affects: 3.0.1

Attachments:

Referenced from: commits b67b5ec, 65e8844

@spring-projects-issues
Copy link
Collaborator Author

Peter Oxenham commented

Patched DispatcherPortlet

@spring-projects-issues
Copy link
Collaborator Author

Peter Oxenham commented

Please ignore my patch as it really doesn't cater for all usage scenarios, specifically when two or more controllers are defined for the one portlet. In this case the getHandler() method returned in the doRenderService is not guaranteed to be the same one returned in the doActionService(). Therefore the @ExceptionHandler method will not be invoked.

Here's a use case

Two controllers (A & B) and defined in a spring portlet XML file.

@Controller
class A {

@RenderMapping
public String defaultRender() {}

}

@Controller
class B {

@RenderMapping(params="action=stuff")
public String renderStuff() {}

@ActionMapping(params="action=stuff")
public String actionStuff() {}

@ExceptionHandler
public ModelAndView handleException() {}
}

If an exception is thrown in actionStuff then handleException is never executed. Even with the patch I supplied, the handler returned by getHandler is for A not B

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

As of Spring 3.0.2, we are passing the handler instance into HandlerExceptionResolver even for an action exception, and we're also applying the HandlerInterceptor preHandleRender phase in such a case (which we didn't before).

However, this is always going to be about the handler instance that defines the render handler method that is about to be invoked. @ExceptionHandler only applies to render and resource phases, which is also explicitly defined in its javadoc now. Note that this is equivalent to the HandlerExceptionResolver interface. The rationale behind that is that exception handling in a web environment primarily means rendering error pages, or sending back some other kind of error response - which can only be done in the render phase. In other words, the kind of exceptions that you are applying @ExceptionHandler to should have an immediate rendering effect; if it's rather a control flow exception that you're dealing with, use a try-catch block within the action handler method instead.

Juergen

@spring-projects-issues spring-projects-issues added type: bug A general bug in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.0.2 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants