Browse Source

Polish

See gh-33127
pull/33350/head
Stéphane Nicoll 2 years ago
parent
commit
51641ece72
  1. 5
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java
  2. 22
      spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientResponseExceptionTests.java

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

@ -274,10 +274,11 @@ public class WebClientResponseException extends WebClientException { @@ -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() {

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

@ -20,6 +20,11 @@ import org.junit.jupiter.api.Test; @@ -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 { @@ -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 { @@ -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");
}
}

Loading…
Cancel
Save