ByeBye
ByeBye

Reputation: 6956

onErrorMap with onErrorContinue

I am learning about error handling using Flux/Mono and have this problem:

Flux.just("AAA", "BBB", "AAA", "CCC")
    .map(s -> {
        if (s.equals("AAA")) {
            throw new IllegalArgumentException();
        }
        return s;
    })
    .map(s -> s + "-LOL")
    .onErrorMap(IllegalArgumentException.class, cause -> new IllegalStateException())
    .onErrorContinue(IllegalArgumentException.class, (e, a) -> System.out.println("Found IllegalArgumentException" + e))
    .onErrorContinue(IllegalStateException.class, (e, a) -> System.out.println("Found IllegalStateException" + e))
    .collectList()
    .block();

What I am trying to do is map an error exception to another exception and then I want to handle it in a specific way.

In my code, I see that onErrorContinue is triggered only for IllegalArgumentException, but I don't understand why - I've just mapped the error to IllegalStateException

Upvotes: 2

Views: 3087

Answers (1)

Simon Baslé
Simon Baslé

Reputation: 28351

If you are starting with reactive programming, I'm urging you to avoid onErrorContinue. This is a very misleading operator that is intended for advanced users with very good grasp of the internal workings of operators. I regret adding that one every time it is mentioned, and am thinking about hiding it a bit more from the public API, because it is too easily assumed to be as straightforward as eg. onErrorResume.

Consider onErrorReturn, onErrorResume, doOnError and subscribe(valueHandler, error -> doSomethingWithError(error)) instead.

Upvotes: 5

Related Questions