package de.juplo.demo;
+import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import org.springframework.web.reactive.function.client.WebClientResponseException.NotFound;
+import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Mono;
.onErrorResume(t ->
{
if(t.getClass().equals(NotFound.class))
- return Mono.error(t);
+ return Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND, "Cause: " + t.getMessage(), t));
if(!(t instanceof WebClientResponseException))
return
Mono.error(
- WebClientResponseException.create(
- 500,
- "Internal Server Error - Cause: " + t.getMessage(),
- null,
- null,
- null));
+ new ResponseStatusException(
+ HttpStatus.INTERNAL_SERVER_ERROR,
+ "Cause: " + t.getMessage(),
+ t));
return
Mono.error(
- WebClientResponseException.create(
- 503,
- "Service Unavailable - Cause: " + t.getMessage(),
- null,
- null,
- null));
+ new ResponseStatusException(
+ HttpStatus.SERVICE_UNAVAILABLE,
+ "Cause: " + t.getMessage(),
+ t));
});
}
}
import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
+import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;
.jsonPath("message").isEqualTo("Required String parameter 'path' is not present")
.jsonPath("timestamp").exists();
}
+
+ @DisplayName("Remote-Server answers with 404: Not Found")
+ @Test()
+ void testResponseNotFound()
+ {
+ when(service.getRemoteText("foo")).thenReturn(Mono.error(exception(404)));
+ webClient
+ .get()
+ .uri("/?path=foo")
+ .header("Accept", MediaType.TEXT_PLAIN_VALUE)
+ .exchange()
+ .expectStatus().isNotFound()
+ .expectBody()
+ .jsonPath("status").isEqualTo(404)
+ .jsonPath("error").isEqualTo("Not Found")
+ .jsonPath("path").isEqualTo("/")
+ .jsonPath("timestamp").exists();
+ }
+
+
+ WebClientResponseException exception(int status)
+ {
+ return WebClientResponseException.create(status, "MESSAGE", null, null, null);
+ }
}
import static org.mockito.Mockito.when;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.WebClientResponseException;
-import org.springframework.web.reactive.function.client.WebClientResponseException.InternalServerError;
-import org.springframework.web.reactive.function.client.WebClientResponseException.NotFound;
-import org.springframework.web.reactive.function.client.WebClientResponseException.ServiceUnavailable;
+import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
StepVerifier
.create(result)
- .expectError(NotFound.class)
+ .expectErrorSatisfies((t) ->
+ {
+ assertThat(t).isInstanceOf(ResponseStatusException.class);
+ ResponseStatusException status = (ResponseStatusException)t;
+ assertThat(status.getStatus()).isEqualTo(HttpStatus.NOT_FOUND);
+ assertThat(t.getMessage()).startsWith("404 NOT_FOUND \"Cause: 404 MESSAGE\";");
+ })
.verify();
}
.create(result)
.expectErrorSatisfies((t) ->
{
- assertThat(t).isInstanceOf(ServiceUnavailable.class);
+ assertThat(t).isInstanceOf(ResponseStatusException.class);
+ ResponseStatusException e = (ResponseStatusException)t;
+ assertThat(e.getStatus()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
assertThat(t.getMessage())
.startsWith(
- "503 Service Unavailable - Cause: " +
- Integer.toString(status.value()));
+ "503 SERVICE_UNAVAILABLE \"Cause: " +
+ Integer.toString(status.value()) +
+ " MESSAGE\";");
})
.verify();
}
.create(result)
.expectErrorSatisfies((t) ->
{
- assertThat(t).isInstanceOf(InternalServerError.class);
- assertThat(t.getMessage()).isEqualTo("500 Internal Server Error - Cause: Boom!");
+ assertThat(t).isInstanceOf(ResponseStatusException.class);
+ ResponseStatusException e = (ResponseStatusException)t;
+ assertThat(e.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
+ assertThat(t.getMessage()).startsWith("500 INTERNAL_SERVER_ERROR \"Cause: Boom!\";");
})
.verify();
}
WebClientResponseException exception(int status)
{
- return WebClientResponseException.create(status, "", null, null, null);
+ return WebClientResponseException.create(status, "MESSAGE", null, null, null);
}
}