Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

java - Catch all kinds of errors during the requests with Spring Webclient

I try to catch any kind of exceptions while doing a request and so that I can break the request.

When I cause a timeout, it perfectly gets catched. But when I do a request to a server which I don't know what it returns as response; I see in the console the following error but it doesn't get catched inside try/catch:

16:02:06.698 [reactor-http-nio-1] WARN  i.n.channel.DefaultChannelPipeline - An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
io.netty.channel.ChannelPipelineException: reactor.netty.resources.PooledConnectionProvider$PooledConnectionAllocator$PooledConnectionInitializer.handlerAdded() has thrown an exception; removed.
    at io.netty.channel.DefaultChannelPipeline.callHandlerAdded0(DefaultChannelPipeline.java:624)
    at io.netty.channel.DefaultChannelPipeline.access$100(DefaultChannelPipeline.java:46)
    at io.netty.channel.DefaultChannelPipeline$PendingHandlerAddedTask.execute(DefaultChannelPipeline.java:1463)
    at io.netty.channel.DefaultChannelPipeline.callHandlerAddedForAllHandlers(DefaultChannelPipeline.java:1115)
    at io.netty.channel.DefaultChannelPipeline.invokeHandlerAddedIfNeeded(DefaultChannelPipeline.java:650)
    at io.netty.channel.AbstractChannel$AbstractUnsafe.register0(AbstractChannel.java:502)
    at io.netty.channel.AbstractChannel$AbstractUnsafe.access$200(AbstractChannel.java:417)
    at io.netty.channel.AbstractChannel$AbstractUnsafe$1.run(AbstractChannel.java:474)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:164)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.util.NoSuchElementException: reactor.netty.resources.PooledConnectionProvider$PooledConnectionAllocator$PooledConnectionInitializer
    at io.netty.channel.DefaultChannelPipeline.getContextOrDie(DefaultChannelPipeline.java:1082)
    at io.netty.channel.DefaultChannelPipeline.remove(DefaultChannelPipeline.java:417)
    at reactor.netty.resources.PooledConnectionProvider$PooledConnectionAllocator$PooledConnectionInitializer.handlerAdded(PooledConnectionProvider.java:284)
    at io.netty.channel.AbstractChannelHandlerContext.callHandlerAdded(AbstractChannelHandlerContext.java:938)
    at io.netty.channel.DefaultChannelPipeline.callHandlerAdded0(DefaultChannelPipeline.java:609)
    ... 15 common frames omitted 

The service class:

@Service
public class TestService {

    public void getEmp() {
        final HttpClient httpClient = HttpClient
                .create()
                .tcpConfiguration(client ->
                        client
                                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100000)
                                .doOnConnected(conn -> conn
                                        .addHandlerLast(new ReadTimeoutHandler(100000, TimeUnit.MILLISECONDS))
                                        .addHandlerLast(new WriteTimeoutHandler(100000, TimeUnit.MILLISECONDS))
                                )
                );

        WebClient client = WebClient.builder()
                .baseUrl("xxx")
                .defaultHeaders(headers -> {
                    headers.setBasicAuth("xxx", "xxx");
                })
                .clientConnector(new ReactorClientHttpConnector(httpClient))
                .build();

        try {
            client
                    .get()
                    .uri("/emps/{cp}", "xxx")
                    .retrieve()
                    .onStatus(HttpStatus::is4xxClientError,
                            error -> Mono.error(new Exception("API not found")))
                    .onStatus(HttpStatus::is5xxServerError,
                            error -> Mono.error(new Exception("Server is not responding")))
                    .onStatus(HttpStatus::isError,
                            error -> Mono.error(new Exception("General error")))
                    .bodyToMono(AceResponse.class)
                    .doOnError(error -> Mono.error(new Exception("General error")))
                    .block();
        } catch (Exception e) {
            System.out.println("MY EXCEPTION: " + e.getMessage());  // CATCH ANY ERROR HERE
        }
    }
}

I just call it inside a controller like testService.getEmp(); . Could you please help me on catching any kind of exceptions with my webclient?

question from:https://stackoverflow.com/questions/65830487/catch-all-kinds-of-errors-during-the-requests-with-spring-webclient

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...