Browse Source

Fix UnprocessableContent support in WebClientResponseException

Prior to this commit, `WebClientResponseException` would only support
the deprecated "unprocessable entity" status.
This commit adds the missing support for "unprocessable content" when
creating exceptions with `WebClientResponseException#create`.

Fixes gh-35802
pull/35813/head
Brian Clozel 1 month ago
parent
commit
89e0273244
  1. 35
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java
  2. 21
      spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientResponseExceptionTests.java

35
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java

@ -314,6 +314,7 @@ public class WebClientResponseException extends WebClientException { @@ -314,6 +314,7 @@ public class WebClientResponseException extends WebClientException {
case GONE -> new WebClientResponseException.Gone(statusText, headers, body, charset, request);
case UNSUPPORTED_MEDIA_TYPE -> new WebClientResponseException.UnsupportedMediaType(statusText, headers, body, charset, request);
case TOO_MANY_REQUESTS -> new WebClientResponseException.TooManyRequests(statusText, headers, body, charset, request);
case UNPROCESSABLE_CONTENT -> new WebClientResponseException.UnprocessableContent(statusText, headers, body, charset, request);
case UNPROCESSABLE_ENTITY -> new WebClientResponseException.UnprocessableEntity(statusText, headers, body, charset, request);
case INTERNAL_SERVER_ERROR -> new WebClientResponseException.InternalServerError(statusText, headers, body, charset, request);
case NOT_IMPLEMENTED -> new WebClientResponseException.NotImplemented(statusText, headers, body, charset, request);
@ -340,7 +341,7 @@ public class WebClientResponseException extends WebClientException { @@ -340,7 +341,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.BAD_REQUEST.value(), statusText, headers, body, charset, request);
super(HttpStatus.BAD_REQUEST, statusText, headers, body, charset, request);
}
}
@ -356,7 +357,7 @@ public class WebClientResponseException extends WebClientException { @@ -356,7 +357,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.UNAUTHORIZED.value(), statusText, headers, body, charset, request);
super(HttpStatus.UNAUTHORIZED, statusText, headers, body, charset, request);
}
}
@ -371,7 +372,7 @@ public class WebClientResponseException extends WebClientException { @@ -371,7 +372,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.FORBIDDEN.value(), statusText, headers, body, charset, request);
super(HttpStatus.FORBIDDEN, statusText, headers, body, charset, request);
}
}
@ -386,7 +387,7 @@ public class WebClientResponseException extends WebClientException { @@ -386,7 +387,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.NOT_FOUND.value(), statusText, headers, body, charset, request);
super(HttpStatus.NOT_FOUND, statusText, headers, body, charset, request);
}
}
@ -401,7 +402,7 @@ public class WebClientResponseException extends WebClientException { @@ -401,7 +402,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.METHOD_NOT_ALLOWED.value(), statusText, headers, body, charset, request);
super(HttpStatus.METHOD_NOT_ALLOWED, statusText, headers, body, charset, request);
}
}
@ -416,7 +417,7 @@ public class WebClientResponseException extends WebClientException { @@ -416,7 +417,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.NOT_ACCEPTABLE.value(), statusText, headers, body, charset, request);
super(HttpStatus.NOT_ACCEPTABLE, statusText, headers, body, charset, request);
}
}
@ -431,7 +432,7 @@ public class WebClientResponseException extends WebClientException { @@ -431,7 +432,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.CONFLICT.value(), statusText, headers, body, charset, request);
super(HttpStatus.CONFLICT, statusText, headers, body, charset, request);
}
}
@ -446,7 +447,7 @@ public class WebClientResponseException extends WebClientException { @@ -446,7 +447,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.GONE.value(), statusText, headers, body, charset, request);
super(HttpStatus.GONE, statusText, headers, body, charset, request);
}
}
@ -461,7 +462,7 @@ public class WebClientResponseException extends WebClientException { @@ -461,7 +462,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value(), statusText, headers, body, charset, request);
super(HttpStatus.UNSUPPORTED_MEDIA_TYPE, statusText, headers, body, charset, request);
}
}
@ -476,7 +477,7 @@ public class WebClientResponseException extends WebClientException { @@ -476,7 +477,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.UNPROCESSABLE_CONTENT.value(), statusText, headers, body, charset, request);
super(HttpStatus.UNPROCESSABLE_CONTENT, statusText, headers, body, charset, request);
}
}
@ -494,7 +495,7 @@ public class WebClientResponseException extends WebClientException { @@ -494,7 +495,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.UNPROCESSABLE_ENTITY.value(), statusText, headers, body, charset, request);
super(HttpStatus.UNPROCESSABLE_ENTITY, statusText, headers, body, charset, request);
}
}
@ -509,7 +510,7 @@ public class WebClientResponseException extends WebClientException { @@ -509,7 +510,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.TOO_MANY_REQUESTS.value(), statusText, headers, body, charset, request);
super(HttpStatus.TOO_MANY_REQUESTS, statusText, headers, body, charset, request);
}
}
@ -528,7 +529,7 @@ public class WebClientResponseException extends WebClientException { @@ -528,7 +529,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.INTERNAL_SERVER_ERROR.value(), statusText, headers, body, charset, request);
super(HttpStatus.INTERNAL_SERVER_ERROR, statusText, headers, body, charset, request);
}
}
@ -543,7 +544,7 @@ public class WebClientResponseException extends WebClientException { @@ -543,7 +544,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.NOT_IMPLEMENTED.value(), statusText, headers, body, charset, request);
super(HttpStatus.NOT_IMPLEMENTED, statusText, headers, body, charset, request);
}
}
@ -558,7 +559,7 @@ public class WebClientResponseException extends WebClientException { @@ -558,7 +559,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.BAD_GATEWAY.value(), statusText, headers, body, charset, request);
super(HttpStatus.BAD_GATEWAY, statusText, headers, body, charset, request);
}
}
@ -573,7 +574,7 @@ public class WebClientResponseException extends WebClientException { @@ -573,7 +574,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.SERVICE_UNAVAILABLE.value(), statusText, headers, body, charset, request);
super(HttpStatus.SERVICE_UNAVAILABLE, statusText, headers, body, charset, request);
}
}
@ -588,7 +589,7 @@ public class WebClientResponseException extends WebClientException { @@ -588,7 +589,7 @@ public class WebClientResponseException extends WebClientException {
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
@Nullable HttpRequest request) {
super(HttpStatus.GATEWAY_TIMEOUT.value(), statusText, headers, body, charset, request);
super(HttpStatus.GATEWAY_TIMEOUT, statusText, headers, body, charset, request);
}
}

21
spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientResponseExceptionTests.java

@ -16,7 +16,14 @@ @@ -16,7 +16,14 @@
package org.springframework.web.reactive.function.client;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import static org.assertj.core.api.Assertions.assertThat;
@ -24,6 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -24,6 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link WebClientResponseException}.
*
* @author Simon Baslé
* @author Brian Clozel
*/
class WebClientResponseExceptionTests {
@ -90,4 +98,17 @@ class WebClientResponseExceptionTests { @@ -90,4 +98,17 @@ class WebClientResponseExceptionTests {
assertThat(ex).hasMessage("500 reasonPhrase").hasRootCauseMessage("example cause");
}
@ParameterizedTest
@MethodSource("httpStatusValues")
void createExceptionWithStatus(HttpStatus status) {
WebClientResponseException exception = WebClientResponseException
.create(status, "reasonPhrase", new HttpHeaders(), new byte[0], null, null);
assertThat(exception.getStatusCode()).isEqualTo(status);
}
static Stream<HttpStatus> httpStatusValues() {
return Stream.of(HttpStatus.values());
}
}

Loading…
Cancel
Save