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);
});
}
Comment From: poutsma
I think you should be able to achieve your goal by using the builder, like so:
if (headers.getFirst("error").equals("true")) {
return ClientResponse.from(clientResponse)
.statusCode(BAD_GATEWAY)
.build()
.createException()
.flatMap(ex -> Mono.error(ex));
}
else {
return Mono.just(clientResponse);
}
Comment From: rstoyanchev
Don't you also need to set the body?
ClientResponse.from(clientResponse)
.statusCode(BAD_GATEWAY)
.body(clientResponse.bodyToFlux(DataBuffer.class))
.build()
.createException()
.flatMap(ex -> Mono.error(ex));
Comment From: poutsma
Good point, I forgot that from does not copy the body.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: membersound
This is great, but how could I add a custom error text into the WebClientResponseException created with this builder?
Comment From: membersound
It would still be nice if one could just supply a custom statusCode during createException() so that the exception generating code is inherited from ClientResponse, but simply the code is changed. Because bugs are likely being introduced by eg forgetting to set the body, as seen above.
Comment From: rstoyanchev
As an API I'm not keen on exposing such a method at this level. It is an exceptional situation, not something that should be a common case. Furthermore it leads down a path of having more overloaded methods. You've already mentioned a custom error text in addition to the status code, and even for the status code, it could be HttpStatus or a raw status value.
ClientResponse provides a method to create an exception from its internal state. If necessary you can then map it to a different exception and change any of the bits:
response.createException().map(ex ->
new WebClientResponseException(
502, "custom status", ex.getHeaders(), ex.getResponseBodyAsByteArray(), UTF_8))
This gives you full control over the mapping and is more readable in the sense that it's clear what's being done vs a createException(HttpStatus) which raises questions.
The only small thing here is that WebClientResponseException doesn't expose its responseCharset field. This is something we can correct.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.