Skip to content
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

WebClient: wrap connection exceptions #23842

Closed
puce77 opened this issue Oct 21, 2019 · 1 comment
Closed

WebClient: wrap connection exceptions #23842

puce77 opened this issue Oct 21, 2019 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@puce77
Copy link

puce77 commented Oct 21, 2019

When calling a remote service, it's important to know if an exception happened before sending the request or after receiving the response.

Unfortunately, not all exceptions are wrapped by WebClientResponseException, WebClientException or another subclass of WebClientException when calling webClient.retrieve().bodyToFlux() resp. webClient.retrieve().bodyToMono()

E.g.:
Before request:

  • java.net.ConnectException
  • io.netty.channel.ConnectTimeoutException
  • io.netty.handler.timeout.ReadTimeoutException

After response:

  • org.springframework.core.codec.DecodingException

org.springframework.core.codec.CodecException could happen before request and after response, I guess. How can we detect this?

I also might have missed some exceptions.

Exception handling would be much easier if the following rule could be implemented:

if the exception happens after the response -> wrap the exception with a WebClientResponseException, so we don't loose the http status code and the payload

else wrap the exception with a WebClientException or another subclass.

I'm not sure if this a bug or an enhancement request.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 21, 2019
@poutsma poutsma added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement labels Nov 5, 2019
@poutsma poutsma self-assigned this Nov 7, 2019
@poutsma poutsma added this to the 5.2.2 milestone Nov 7, 2019
@rstoyanchev rstoyanchev removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 8, 2019
@poutsma poutsma removed this from the 5.2.2 milestone Nov 19, 2019
@poutsma
Copy link
Contributor

poutsma commented Nov 19, 2019

Team Decision: It makes sense to handle, wrap and re-emit the different connection exceptions, but doing so will change backwards compatibility and might break users' existing connection-handling code. So we cannot do this until 5.3 at least.

As for wrapping in a WebClientResponseException: we can only consume the response body once, so once the decoder has partially done so but throws a DecodingException, we cannot consume that body again to put it in the WebClientResponseException. So that would only leave the status code, which is probably not that useful, as it is probably going to be 200.

Finally, I am not so sure if the wrapping and re-throwing pattern is as useful for onError* method as it is for catch clauses. In a catch clause, it is convenient to catch a general exception for all possible errors, but I don't think the same holds true for an onErrorResume.

@poutsma poutsma changed the title WebClient: wrap exceptions WebClient: wrap connection exceptions Nov 19, 2019
@poutsma poutsma added this to the 5.3 M1 milestone Nov 19, 2019
@poutsma poutsma modified the milestones: 5.3 M1, 5.3 M2 Jun 22, 2020
@poutsma poutsma modified the milestones: 5.3 M2, 5.3 RC1 Aug 3, 2020
poutsma added a commit to poutsma/spring-framework that referenced this issue Sep 2, 2020
This commit makes sure that exceptions emitted by WebClient are wrapped
by WebClientExceptions:

- Exceptions emitted by the ClientHttpConnector are wrapped in a new
  ConnectException.

- Exceptions emmitted after a response is received are wrapped in a
  WebClientResponseException

Closes spring-projectsgh-23842
wilkinsona added a commit to spring-projects/spring-boot that referenced this issue Sep 14, 2020
jgrandja added a commit to spring-projects/spring-security that referenced this issue Sep 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants