Handling errors

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.214.30