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

Support Predicate- based mappedHandler config in AbstractHandlerExceptionResolver #26772

Closed
haupv1m opened this issue Apr 7, 2021 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@haupv1m
Copy link

haupv1m commented Apr 7, 2021

Affects: Spring 5.3.5

I'm using Spring MVC framework with functional controller (HandlerFunction, HandlerFunctionFilter, are landed on Spring 5.2). While using it, I encountered problem with global exception capture.
If I use onError for handling exception, I still need to use @ControllerAdvice and @ErrorException for non-path exceptions (AccessDeniedException,NotFoundHandlerException, ...). This is inconsistency. After some research, I decide to use mappedHandlerClasses of ExceptionHandlerExceptionResolver to handle exception in functional controller. However, when enable setting mappedHandlerClasses, ExceptionHandlerExceptionResolver cannot handle non-path exceptions (AccessDeniedException,NotFoundHandlerException, ...).

In my opinion, ExceptionHandlerExceptionResolver should capture both HandlerMethod (annotation controller) and HandlerFunction (functional controller) in Spring MVC. Or another suggestion, it's mappedHandlerClasses should not effect to capability of handling HandlerMethod of ExceptionHandlerExceptionResolver.

Or is there any clever way to resolve this issue? Currently, I work around by creating another ExceptionHandlerExceptionResolver with mappedHandlerClasses for function controller exception handler, beside original ExceptionHandlerExceptionResolver.

@poutsma
Copy link
Contributor

poutsma commented Apr 8, 2021

If you create your own implementation of HandlerExceptionResolver, and make sure it acts before the default ones, you can handle exceptions from both functional endpoints as well as annotated handler methods. There is an article that describes custom HandlerExceptionResolver here: https://www.baeldung.com/exception-handling-for-rest-with-spring

In my opinion, ExceptionHandlerExceptionResolver should capture both HandlerMethod (annotation controller) and HandlerFunction (functional controller) in Spring MVC

One of the underlying principles of the functional endpoints is that they do not require reflection to operate. Supporting @ExceptionHandler would violate that principle.

@poutsma poutsma added the status: waiting-for-feedback We need additional information before we can continue label Apr 8, 2021
@haupv1m
Copy link
Author

haupv1m commented Apr 8, 2021

@poutsma There are some points I have to be cleared:

  • Firstly, as I mentioned, I already workaround for handling exception in function controller by implementing a custom HandlerExceptionResolver and it works well for capture functional handler exception, beside ExceptionHandlerExceptionResolver. My problem is NOT how to implement a custom HandlerExceptionResolver.
  • Because I need handle exception when using functional handler, I have 2 approaches:
    • using onError for route exception, combine with @ControllerAdvice and @ErrorException for non-path exceptions (AccessDeniedException,NotFoundHandlerException, ...). This way is inconsistency (request exceptions are handling in 2 ways).
    • or adding a custom HandlerExceptionResolver to capture exception from functional handler (this resolver have same logic as ExceptionHandlerExceptionResolver) and all exception will be handled in @ExceptionHandler. And I choose this way.

One of the underlying principles of the functional endpoints is that they do not require reflection to operate

I agree with this. However, my situation is because of lacking of supporting in uniform handling global exception mechanism of functional controller usage in Spring MVC (as mention in option 1). Moreover, I do NOT suggest functional handler require reflection to operate. I suggest the old exception handler approach (HandlerExecptionResolver) should support both annotation and functional controller until we have a handling global exception mechanism of functional controller to match with new execption handler approach (onError).

I think, because default ExceptionHandlerExceptionResolver's purpose is mapping caputured exception with @ExceptionHandler methods for resolving, not about exception source, and because functional handler and annotated handler are both principle components of Spring MVC, ExceptionHandlerExceptionResolver should be capability of capturing both exceptions from annotated handler methods and functional handlers (currently, ExceptionHandlerExceptionResolver only work with either HandlerMethod or mappedHandlerClasses).

Note, I'm using function controller in Spring MVC (functional approach similar as functional endpoints in Spring WebFlux), but not functional endpoints in Spring WebFlux.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Apr 8, 2021
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Nov 8, 2021
@sbrannen sbrannen changed the title Suggestion way to handle both exceptions from HandlerFunction and non-path exception in Spring MVC Suggestion for how to handle both exceptions from HandlerFunction and non-path exception in Spring MVC Jan 10, 2023
@rstoyanchev rstoyanchev added this to the 6.1.x milestone Jan 16, 2023
@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Jan 16, 2023
@rstoyanchev rstoyanchev self-assigned this Dec 5, 2023
@rstoyanchev rstoyanchev modified the milestones: 6.1.x, 6.1.2 Dec 5, 2023
@rstoyanchev rstoyanchev changed the title Suggestion for how to handle both exceptions from HandlerFunction and non-path exception in Spring MVC Support Predicate- based mappedHandler config in AbstractHandlerExceptionResolver Dec 5, 2023
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: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants