javaspringspring-webfluxproject-reactor

Difference between Mono.error(Throwable throwable) and Mono.error(Supplier<? extends Throwable> errorSupplier)


Can I visually see the difference between the Mono.error(Throwable throwable) and Mono.error(Supplier<? extends Throwable> errorSupplier). I understand that these are eager and lazy loading. The exception is created when subscribe method is called in lazy and exception is created as soon as onRequest signal is passed in eager, but in term of output I can't observe any difference.


Solution

  • At the end, the effect will be the same, in case of error. If no error (successful) then you can notice that no exception is created for the variant with a supplier, but one is created with the plain variant.

    Assuming the exception is a custom you wrote, you can output a log in it constructor, then you could see when it is created. If filtering all logs to DEBUG and more, you could see a difference in the logs orders.

    class MyException extends RuntimeException {
        MyException() {
            log.info("creating my exception");
        }
    }
    

    Additionally or alternatively, you can put a log in the supplier you gave, so you see when the supplier is involved:

    mono.error(() -> {
        log.info("supplying my exception");
        return MyException();
    });