I have a service written using spring web. However, to perform our http calls we have decided to use WebClient (I do not have the option to use RestTemplate or convert the service to use WebFlux). So, in the block code below:
public List<SomeObject> getObject(final String param1, final String param2) {
return openApiGenerateClient.getSomething(param1, param2)
.collectList()
.onErrorResume(WebClientResponseException.BadRequest.class, t -> error(new CustomException(t)))
.block();
}
I have a client side code generated by openApi which uses WebClient to perform the http request. The response is a Flux. I would like to "catch" specific errors, such as "Bad Request" and convert into my custom error to be handled in my Controller Advice. This code above although it compiles, when tested always return the exception of type WebClientResponseException.BadRequest and not my CustomException.
I have tried other Flux operations such as .onErrorReturn . I know I can simply wrap my call in a try and catch and then handle the errors, but I would like to know if there is a cleaner way of doing, without having some ugly code with multiple catchs.