diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java index 3692fe40e07..4a5310d3a57 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java @@ -274,10 +274,11 @@ public class WebClientResponseException extends WebClientException { @Override public String getMessage() { + String message = String.valueOf(super.getMessage()); if (shouldHintAtResponseFailure()) { - return super.getMessage() + ", but response failed with cause: " + getCause(); + return message + ", but response failed with cause: " + getCause(); } - return super.getMessage(); + return message; } private boolean shouldHintAtResponseFailure() { diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientResponseExceptionTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientResponseExceptionTests.java index e710ad1bb28..76a79a24c6f 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientResponseExceptionTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientResponseExceptionTests.java @@ -20,6 +20,11 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +/** + * Tests for {@link WebClientResponseException}. + * + * @author Simon Baslé + */ class WebClientResponseExceptionTests { @Test @@ -31,34 +36,34 @@ class WebClientResponseExceptionTests { @Test void constructWith1xxStatusCodeAndCauseAdditionalMessage() { - final WebClientResponseException ex = new WebClientResponseException(100, "reasonPhrase", null, null, null); + WebClientResponseException ex = new WebClientResponseException(100, "reasonPhrase", null, null, null); ex.initCause(new RuntimeException("example cause")); assertThat(ex).hasMessage("100 reasonPhrase, but response failed with cause: java.lang.RuntimeException: example cause"); } @Test void constructWith2xxStatusCodeAndCauseAdditionalMessage() { - final WebClientResponseException ex = new WebClientResponseException(200, "reasonPhrase", null, null, null); + WebClientResponseException ex = new WebClientResponseException(200, "reasonPhrase", null, null, null); ex.initCause(new RuntimeException("example cause")); assertThat(ex).hasMessage("200 reasonPhrase, but response failed with cause: java.lang.RuntimeException: example cause"); } @Test void constructWith3xxStatusCodeAndCauseAdditionalMessage() { - final WebClientResponseException ex = new WebClientResponseException(300, "reasonPhrase", null, null, null); + WebClientResponseException ex = new WebClientResponseException(300, "reasonPhrase", null, null, null); ex.initCause(new RuntimeException("example cause")); assertThat(ex).hasMessage("300 reasonPhrase, but response failed with cause: java.lang.RuntimeException: example cause"); } @Test void constructWithExplicitMessageAndNotErrorCodeAdditionalMessage() { - final WebClientResponseException ex = new WebClientResponseException("explicit message", 100, "reasonPhrase", null, null, null); + WebClientResponseException ex = new WebClientResponseException("explicit message", 100, "reasonPhrase", null, null, null); assertThat(ex).hasMessage("explicit message, but response failed with cause: null"); } @Test void constructWithExplicitMessageAndNotErrorCodeAndCauseAdditionalMessage() { - final WebClientResponseException ex = new WebClientResponseException("explicit message", 100, "reasonPhrase", null, null, null); + WebClientResponseException ex = new WebClientResponseException("explicit message", 100, "reasonPhrase", null, null, null); ex.initCause(new RuntimeException("example cause")); assertThat(ex).hasMessage("explicit message, but response failed with cause: java.lang.RuntimeException: example cause") .hasRootCauseMessage("example cause"); @@ -66,22 +71,23 @@ class WebClientResponseExceptionTests { @Test void constructWithExplicitMessageAndErrorCodeAndCauseNoAdditionalMessage() { - final WebClientResponseException ex = new WebClientResponseException("explicit message", 404, "reasonPhrase", null, null, null); + WebClientResponseException ex = new WebClientResponseException("explicit message", 404, "reasonPhrase", null, null, null); ex.initCause(new RuntimeException("example cause")); assertThat(ex).hasMessage("explicit message").hasRootCauseMessage("example cause"); } @Test void constructWith4xxStatusCodeAndCauseNoAdditionalMessage() { - final WebClientResponseException ex = new WebClientResponseException(400, "reasonPhrase", null, null, null); + WebClientResponseException ex = new WebClientResponseException(400, "reasonPhrase", null, null, null); ex.initCause(new RuntimeException("example cause")); assertThat(ex).hasMessage("400 reasonPhrase").hasRootCauseMessage("example cause"); } @Test void constructWith5xxStatusCodeAndCauseNoAdditionalMessage() { - final WebClientResponseException ex = new WebClientResponseException(500, "reasonPhrase", null, null, null); + WebClientResponseException ex = new WebClientResponseException(500, "reasonPhrase", null, null, null); ex.initCause(new RuntimeException("example cause")); assertThat(ex).hasMessage("500 reasonPhrase").hasRootCauseMessage("example cause"); } + }