记录反应链中的什么方法异常

Log what method in reactive chain thew an exception

我的反应链中有一些方法可以抛出异常。如果发生异常 - 我不想继续执行链,并且我想记录 - 究竟是什么操作引发了异常。

我写了一个代码,看起来像这样:

   Mono<Void> mono = Mono.just(10)
            .map(a -> action1(a))
            .doOnError((t) -> System.out.println("action 1 threw an exception"))
            .onErrorStop()
            .map(a -> action2(a)) 
            .doOnError((t) -> System.out.println("action 2 threw an exception"))
            .then();
    
    mono.subscribe();

但是当 action1 抛出异常时 - 我得到以下输出:

action 1 threw an exception
action 2 threw an exception

什么不是我所期望的

  1. 你需要把reactive chain看成一个pipeline或者一个数据flow.Inpipeline,error和其他的一样也是一个data
  2. flatMap() 方法在数据可用时将信号更改为另一个信号(单声道或通量)。
Mono<Void> mono = Mono.just(10)
                .map(a -> action1(a))
                .doOnError((t) -> System.out.println("action 1 threw an exception"))
                .flatMap(a -> Mono.just(a)
                        .map(value -> action2(value))
                        .doOnError((t) -> System.out.println("action 2 threw an exception"))
                        .then()
                )
                .then();
        mono.subscribe();