Monosynchronous Exception Handling

I'm just trying to understand how exception handling works in a reactor library.

Consider the following example:

public class FluxTest {

@Test
public void testIt() throws InterruptedException {
    Scheduler single = Schedulers.single();

    CountDownLatch latch = new CountDownLatch(1);

    Mono.just("hey")
            .subscribeOn(single)
            .doOnError(e -> System.out.println("ERROR1: " + e.getMessage()))
            .doOnTerminate((r, e) -> {
                if (e != null) System.out.println("ERROR3: " + e.getMessage());
                latch.countDown();
            })
            .subscribe(
                    it -> { throw new RuntimeException("Error for " + it); },
                    ex -> { System.out.println("ERROR2: " + ex.getMessage());}
                    );

    latch.await();
}

}

In fact, I do not see any error handling block being executed. The test ends without any message. In addition, I tried to remove the doOnError, doOnTerminateprocessors without any luck.

+4
source share
1 answer

And this is the correct behavior in your case. You create mono from one line of β€œhey” that has no errors. If you try to debug, you will see that the method doOnTerminateis being called with a parameter e = null, and according to the documentation it is being called anyway successor error.

, :

public class FluxTest {
    @Test
    public void testIt() throws InterruptedException {
        Scheduler single = Schedulers.single();

        CountDownLatch latch = new CountDownLatch(1);

        Mono.just("hey")
                .map(this::mapWithException)
                .subscribeOn(single)
                .doOnError(e -> System.out.println("ERROR1: " + e.getMessage()))
                .doOnTerminate((r, e) -> {
                    if (e != null) System.out.println("ERROR3: " + e.getMessage());
                    latch.countDown();
                })
                .subscribe(
                        it -> { throw new RuntimeException("Error for " + it); },
                        ex -> { System.out.println("ERROR2: " + ex.getMessage());}
                        );

        latch.await();
    }

    private String mapWithException(String s) {
        throw new RuntimeException();
    }
}

ERROR1: null
ERROR3: null
ERROR2: null

, onError, mono failed, onTerminate, mono , - errorConsumer subscribe.

+2

Source: https://habr.com/ru/post/1678815/


All Articles