How to handle error while executing Flux.map()
You need flatMap
instead which let's you return an empty sequence if the processing failed:
myflux.flatMap(v -> {
try {
return Flux.just(converter.convertHistoricalCSVToStockQuotation(stock));
} catch (IllegalArgumentException ex) {
return Flux.empty();
}
});
If you want to use Reactor 3's methods for dealing with exceptions, you can use Mono.fromCallable
.
flatMap(x ->
Mono.fromCallable(() -> converter.convertHistoricalCSVToStockQuotation(x))
.flux()
.flatMap(Flux::fromIterable)
.onErrorResume(Flux::empty)
)
Unfortunately there is no Flux.fromCallable
, so assuming the callable returns a list, you have to convert it to a Flux manually.