A WebClient instance allows you to handle errors (the WebClientTestImpl class) in the listMovies() method, as follows:
@Override
public Flux<Movie> listMovies() {
return webClient.get().uri("/")
.retrieve()
.onStatus(HttpStatus::is4xxClientError, clientResponse ->
Mono.error(new SampleException())
)
.onStatus(HttpStatus::is5xxServerError, clientResponse ->
Mono.error(new SampleException())
)
.bodyToFlux(Movie.class);
}
SampleException is a custom exception class that we created by extending the Exception class. We are handling 4xx and 5xx errors, and, when encountered, it sends the custom exception as response.