Closed
Description
It would be nice if you could extend WebClient
with createException(HttpStatus code)
.
Because eg I have an external webservice that I have no control of, and it always returns 200 OK
EVEN in case of error responses in body and an error hint in the header.
So when I evaluate the response with a ExchangeFilterFunction
, I might want to return a clientResponse.createException(BAD_GATEWAY)
based on the received content.
This is impossible by simply calling clientResponse.createException()
, which would create a json exception, BUT with status 200 OK
proxied through!
In my case, I'd like to write as follows:
private static ExchangeFilterFunction errorHandler() {
return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
HttpHeaders headers = clientResponse.headers().asHttpHeaders();
return (headers.getFirst("error").equals("true"))
? clientResponse.createException(BAD_GATEWAY).flatMap(ex -> Mono.error(ex)) //this is not possible atm
: Mono.just(clientResponse);
});
}