How to skip error in reactor? At the first glance, it looks like something trivial, but it could be that you spent a few hours to puzzle it out, and you’ve got no results. Today I will show simple it is.

Let say that I have following reactor/flux stack.

class Foo {
    Flux<Bar> method(SomeRequest request) {
        return Flux.just(request)
                   .flatMap(req -> externalApi.execute(request)) // (1) process this 
                   .flatMap(response -> sideEffectAction(response)) // (2) error here 
                   .map(response -> response.getBar()); // (3) than process this, even if error occurred
    }
    
    private Flux<SomeResponse> sideEffectAction(SomeResponse response) {
        // return flux -> error is possible
    }
}

We would like to execute (1) than (2), and if any error occurs in 2 we need to process (3). In short, we need to skip sideEffectAction error if any error occurs. Simple onErrorContinue would not work, because than point (3) will be omitted. So, how can we achieve that?

class Foo {
    Flux<Bar> method(SomeRequest request) {
        return Flux.just(request)
                   .flatMap(req -> externalApi.execute(request))
                   .flatMap(response -> 
                       sideEffectAction(response)
                           .onErrorReturn(response) // (1) skip error
                   )
                   .map(response -> response.getBar());
    }
    
    private Flux<SomeResponse> sideEffectAction(SomeResponse response) {
        // return flux -> error is possible
    }
}

Yep, that’s it. The nested onErrorReturn (1) supposed to be the most straightforward solution :smile: