Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exceptions can be thrown by exchange() on WebClient?

I've implemented a service which makes ReST calls out to other services to implement part of its functionality. I'm using the reactive WebClient for this, something like:

webClient.post()
    .uri(....)
    .contentType(....)
    .accept(....)
    .header(....)
    .syncBody(someRequestObject)
    .exchange()
    .flatMap(someResponseHandler::handleResponse)
    .doOnError(throwable -> {
        // do interesting things depending on throwable
    })
    .retry(1, this::somePredicateDependingOnThrowable);

Now... I handle HTTP statuses in someResponseHandler::handleResponse, but what I really want to know is, what other kinds of exception/error to expect from the exchange() - i.e.

  • what exceptions/errors do I get if I can't connect to the downstream service at all?
  • What exceptions/errors do I get if the connection attempt times out?
  • What exceptions/errors do I get if I can connect but then the request times out before I get a response?

None of these are HTTP status codes, obviously - but I can't find any documentation to tell me what I can look for. Am I just not looking in the right places? I've had a look through the documentation for the reactive WebClient, and I've had a look through the Reactor Netty Reference Guide, but no luck.

For background, this is important because we do HATEOAS-based service discovery - for some of these exceptions, I want to trigger rediscovery, for some of them, I don't.

like image 524
AndyB Avatar asked Dec 29 '25 08:12

AndyB


1 Answers

I recommend testing your code that uses the WebClient to see how it handles the various scenarios you mentioned. You can test your code against something like MockWebServer easily from unit tests. MockWebServer can simulate most of the errors mentioned here.

Having said that, here's what I have seen in my testing when using WebClient with the ReactorClientHttpConnector. Other connectors may throw slightly different exceptions, but will likely share a super class in the exception class hierarchy as those mentioned below.

Unknown host

java.net.UnknownHostException

Connection refused (port not open on server)

java.net.ConnectException (or subclass)

reactor-netty throws io.netty.channel.AbstractChannel$AnnotatedConnectException

Connect timeout

If you have configured a connect timeout, then you will receive java.net.ConnectException (or subclass)

reactor-netty throws io.netty.channel.ConnectTimeoutException

SSL handshake errors

javax.net.ssl.SSLHandshakeException (or subclass)

Request body encoding error

This varies by the encoder being used, but generally will be org.springframework.core.codec.EncodingException (or subclass)

Some encoders also throw java.lang.IllegalStateException if encoding is configured incorrectly

Response body decoding error

This varies by the decoder being used, but generally will be org.springframework.core.codec.DecodingException (or subclass)

Some decoders also throw java.lang.IllegalStateException if decoding is configured incorrectly

Read Timeout

If using reactor-netty, and you configured a io.netty.handler.timeout.ReadTimeoutHandler, then io.netty.handler.timeout.ReadTimeoutException

If you use the .timeout operator somewhere in the reactive stream call chain, then java.util.concurrent.TimeoutException

Write Timeout

If using reactor-netty, and you configured a io.netty.handler.timeout.WriteTimeoutHandler, then io.netty.handler.timeout.WriteTimeoutException

Connection closed by server prematurely (before response completes)

java.io.IOException (or subclass)

reactor-netty throws reactor.netty.http.client.PrematureCloseException

Others

Any exceptions that occur during your someResponseHandler::handleResponse

like image 177
Phil Clay Avatar answered Dec 30 '25 22:12

Phil Clay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!