Search code examples
javareactive-programmingspring-webfluxproject-reactor

Can't consume Mono.error when subscribed with fromCallable/fromSupplier


I'm trying to wrap method with fromCallable to execute my method from another thread. But when i return as Mono.error, my error consumer not catch any error when i subscribe to it. Instead it only prints Mono.error on my log. Am i doing it wrong?

Mono.fromCallable(()
                -> Mono.error(new Exception("error coming")))
                .subscribeOn(Schedulers.boundedElastic())
                .doOnError(throwable -> LOG.debug("error {}",throwable.getMessage()))
                .log()
                .subscribe(res ->LOG.debug("result {}", res),
                        err -> LOG.error("Error message {}", err.getMessage()));

Solution

  • The Callable passed to Mono.fromCallable(...) should either:

    1. return the element to emit, or
    2. throw an exception (using the throw keyword).

    In other words, the Callable should not return a Mono or Flux, which is what your example is doing.