-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend WebClient with createException(HttpStatus code) #24126
Comments
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);
} |
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)); |
Good point, I forgot that |
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. |
This is great, but how could I add a custom error text into the |
It would still be nice if one could just supply a custom |
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 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 The only small thing here is that |
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. |
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. |
It would be nice if you could extend
WebClient
withcreateException(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 aclientResponse.createException(BAD_GATEWAY)
based on the received content.This is impossible by simply calling
clientResponse.createException()
, which would create a json exception, BUT with status200 OK
proxied through!In my case, I'd like to write as follows:
The text was updated successfully, but these errors were encountered: