2

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

ByeBye
  • 6,650
  • 5
  • 30
  • 63

1 Answers1

5

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.

Simon Baslé
  • 27,105
  • 5
  • 69
  • 70
  • Actually, I used `onErrorResume` after reading https://github.com/reactor/reactor-core/issues/2184 and it is working well for my case. I left the question to at least have a proper answer for this specific case, but thank you for your comment :) – ByeBye Feb 11 '21 at 19:38