Add status handler to recognize unknown status codes outside of 4xx/5… #31202
+48
−3
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.
…xx and throw an UnknownHttpStatusCodeException
org.springframework.web.reactive.function.client.UnknownHttpStatusCodeException claims in its Javadoc that it will be thrown to signal that "an unknown (or custom) HTTP status code is received". Up until Spring 5/Spring Boot 2 this was indeed true. We recently upgraded to Spring Boot 3/Spring 6, and our code that expected to receive this exception no longer works.
It turns out that the DEFAULT_STATUS_HANDLER in DefaultWebClient tests HttpStatusCode.isError as a predicate before calling DefaultClientResponse#createException. createException is the only place UnknownHttpStatusException is instantiated.
This is a behavior change from Spring 5, since now only 4xx/5xx custom status codes can lead to a UnknownHttpStatusException, in spite of what the exception's javadoc says.
This PR splits DEFAULT_STATUS_HANDLER into one for errors, and one for unknown codes. Both call the same function, with different predicates - one calls isError(), the other calls a new function isWellKnownStatusCode(). I kept the logic for both WebClientResponseException and UnknownHttpStatusCodeException in createException(), since there still can be a custom 4xx/5xx code. I also provide a test that validates the behavior for custom codes outside of that range, in addition to the existing test which only verified for a custom 5xx code.