title: For Spring WebFlux's WebClient how to catch Netty's Exception such as ProxyConnectException
body: <p>I'd like to change Apache HttpComponents Client to Spring WebFlux's WebClient in my application.But I can't catch the exceptions from  Netty,such as <code>io.netty.handler.proxy.ProxyConnectException</code>.</p>
  
  <p>I have try the <code>doOnError</code> operator,but for these exceptions,they are can't be catched.</p>
  
  <pre><code>var timeout = Duration.ofSeconds(3);
          if (proxy != null) {
              HttpClient httpClient = HttpClient.create()
                  .tcpConfiguration(tcpClient -&gt;
                      tcpClient.proxy(py -&gt; py.type(ProxyProvider.Proxy.HTTP).host(proxy.getHost()).port(proxy.getPort()).username(proxy.getUser()).password(tmp -&gt; proxy.getPass())).doOnConnected(conn -&gt; conn
                          .addHandlerLast(new ReadTimeoutHandler((int) (timeout.toSeconds() / 2)))
                          .addHandlerLast(new WriteTimeoutHandler((int) (timeout.toSeconds() / 2)))));
              ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
  
              ExchangeStrategies strategies = ExchangeStrategies
                  .builder()
                  .codecs(clientDefaultCodecsConfigurer -&gt; {
                      clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper, MediaType.APPLICATION_JSON));
                      clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(objectMapper, MediaType.APPLICATION_JSON));
  
                  }).build();
  
              WebClient webClient = WebClient
                  .builder()
                  .exchangeStrategies(strategies)
                  .clientConnector(connector)
                  .build();
              ClientResponse httpException = ClientResponse.create(org.springframework.http.HttpStatus.REQUEST_TIMEOUT).build();
  
  </code></pre>
  
  <pre><code>
        webClient
        .get()
        .uri("my_uri")
        .accept(MediaType.ALL)
        .header("my_header_key", "my_header_value")
        .exchange().doOnError(ProxyConnectException.class, throwable -&gt; {
                      logger.warn("ProxyConnectException");
                  })
        .onErrorReturn(httpException)
        .doOnError(ConnectTimeoutException.class, e -&gt; {
                      logger.warn("ConnectTimeoutException");
                  })
        .doOnError(ReadTimeoutException.class, e -&gt; {
                      logger.warn("ReadTimeoutException");
                  })
        .doOnError(SSLException.class, e -&gt; {
                      logger.warn("SSLException");
                  })
        .doOnError(Exception.class, e -&gt; {
                      logger.warn("Exception");
                  })
        .onErrorReturn(httpException)
        .block();
  </code></pre>
  
  <p>What I want is just print the log and return the <code>httpException</code> object.But the result is not only what I expected,my logs is printed, my object is returned,but I can also see some exception info as follows.Is there any way to avoid these exceptions?</p>
  
  <pre><code>io.netty.handler.proxy.ProxyConnectException: http, none, /XXXX:XX =&gt; XXXX:XX, timeout
      at io.netty.handler.proxy.ProxyHandler$2.run(ProxyHandler.java:201)
      at io.netty.util.concurrent.PromiseTask$RunnableAdapter.call(PromiseTask.java:38)
      at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:127)
      at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
      at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
      at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:335)
      at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:897)
      at java.base/java.lang.Thread.run(Thread.java:834)
  ERROR [reactor-http-epoll-5] HttpClientConnect --- [id: 0x3610bfd0, L:/XXXX:XX ! R:XXXX/XXXX:XX] The connection observed an error
  java.nio.channels.ClosedChannelException: null
      at io.netty.handler.ssl.SslHandler.channelInactive(...)(Unknown Source)```
  </code></pre>