It feels like this should be simple, so I might be missing something obvious. I have a simple example case where I have an error Mono, and I would like to have exception class specific handling with a generic handler.
Mono.error(new RuntimeException())
.doOnError(RuntimeException.class, e -> System.out.println("Caught RuntimeException"))
.doOnError(Throwable.class, e -> System.out.println("Caught Throwable"))
.block();
output: Caught RuntimeException
Caught Throwable
The problem is that both consumers will get called (the one with RuntimeException and the generic one with Throwable). Is there a (clean) way to avoid calling the generic one if a more specific one was already called?
If there is a way to recover from an error, use onErrorResume function instead.
Mono.error(new RuntimeException())
.flatMap(k -> callExternalService(k)
.onErrorResume(RuntimeException.class, this::recoverFromRuntimeExeption)
.onErrorResume(Throwable.class, this::recoverFromThrowable)
);
This way you'll change the execution path of your Mono from error to success and subsequent doOnError
won't be called.