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

Order ProblemDetailsExceptionHandler beans #36288

Conversation

mzeijen
Copy link
Contributor

@mzeijen mzeijen commented Jul 10, 2023

In this pull request I added an explicit zero order to the ProblemDetailsExceptionHandler beans for WebMVC and Webflux.

Without the order, it means these beans automatically have the lowest precedence, and they will usually be executed last. This can be problematic when someone would like to create a “catch-all” @ExceptionHandler in a @ControllerAdvice bean. Meaning an exception handler that should handle any exception that was not handled by any other exception handler with.
Such an exception handler should have the absolute lowest precedence, and the ProblemDetailsExceptionHandler must have a higher precedence. However, because both now have the same precedence, it is not a guarantee that this will be the case. In my tests, my own catch-all exception handler would always start handling those exceptions that should be handled by the ProblemDetailsExceptionHandler.

The workaround is to create custom beans that implement ResponseEntityExceptionHandler and give those an order that has a higher precedence then my catch-all exception handler. However, I don’t think this should be necessary.

Because the ResponseEntityExceptionHandler has a defined list of exceptions it is going to handle, there should be no issue in it having a specific order by default. If it has the order 0 then anyone can easily create an exception handler that has a higher or lower precedence.

I created a test which shows the issue, and the current workaround:

https://github.com/mzeijen/spring-boot-problem-support/blob/main/src/test/java/com/example/demo/ResponseEntityExceptionHandlerOrderingTest.java

This test has nested test classes which show the difference in behavior for both WebMVC and Webflux, when the default ResponseEntityExceptionHandler are used or when the workaround is applied.

In the pull request I added a test that verifies that the ordering takes effect.

Add the `@Order(0)` to the WebMVC and Webflux
`ProblemDetailsExceptionHandler` beans.
This makes it easier to create custom `@ControllerAdvice` beans
that must be ordered after the `ProblemDetailsExceptionHandler`.
Because, it is no longer necessary to create a custom
`ResponseEntityExceptionHandler` bean with an order, that
overrides the `ProblemDetailsExceptionHandler` bean.
@wilkinsona
Copy link
Member

wilkinsona commented Jul 21, 2023

WDYT, @bclozel? This seems sensible to me but there's a small chance that it will be a breaking change. As such, I'd target it at 3.2.x.

@wilkinsona wilkinsona added the for: team-attention An issue we'd like other members of the team to review label Jul 21, 2023
Copy link
Member

@bclozel bclozel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change makes sense and I agree 3.2 should be the target. Thanks @mzeijen !

@bclozel bclozel added type: enhancement A general enhancement and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Aug 1, 2023
@bclozel bclozel added this to the 3.2.x milestone Aug 1, 2023
@snicoll snicoll self-assigned this Aug 2, 2023
@snicoll snicoll modified the milestones: 3.2.x, 3.2.0-M2 Aug 2, 2023
snicoll pushed a commit that referenced this pull request Aug 2, 2023
Add `@Order(0)` to the WebMVC and Webflux
`ProblemDetailsExceptionHandler` beans. This makes it easier to create
custom `@ControllerAdvice` beans that must be ordered before or after
the `ProblemDetailsExceptionHandler`.

See gh-36288
@snicoll snicoll closed this in 4f877db Aug 2, 2023
@snicoll
Copy link
Member

snicoll commented Aug 2, 2023

Thank you @mzeijen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants