3

I want to add a global error filter to my WebClient, so that any exceptions and responses that are not 200 OK result in an exception.

Problem: the following already works, but in case the url host connection is down completely (java.net.ConnectException), then the filter does not execute at all:

WebClient.Builder builder;
builder.filter(errorFilter());

public static ExchangeFilterFunction errorFilter() {
    return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
        if (!clientResponse.statusCode().equals(HttpStatus.OK)) {
            return Mono.error(new MyCustomException());
        }

        return Mono.just(clientResponse);
    });
}

Usage: webClient.post().syncBody(body).retrieve().block();

Question: how can I add a global error filter here that also catches those type of exceptions?

membersound
  • 81,582
  • 193
  • 585
  • 1,120

0 Answers0