-
Notifications
You must be signed in to change notification settings - Fork 40.8k
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
Closed
mzeijen
wants to merge
1
commit into
spring-projects:main
from
mzeijen:order-problem-details-exception-handler
Closed
Order ProblemDetailsExceptionHandler beans #36288
mzeijen
wants to merge
1
commit into
spring-projects:main
from
mzeijen:order-problem-details-exception-handler
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
spring-projects-issues
added
the
status: waiting-for-triage
An issue we've not yet triaged
label
Jul 10, 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
added
the
for: team-attention
An issue we'd like other members of the team to review
label
Jul 21, 2023
bclozel
approved these changes
Aug 1, 2023
There was a problem hiding this 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
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
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
added a commit
that referenced
this pull request
Aug 2, 2023
Thank you @mzeijen! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 theProblemDetailsExceptionHandler
.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 order0
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.