Java – RxJava allows multiple onError calls

RxJava allows multiple onError calls… here is a solution to the problem.

RxJava allows multiple onError calls

I’m trying to allow infinite streams on the next and wrong call.

The following code uses retry() I think this method allows me to see the effect on onNext of all 3 calls, but no other calls after the error.

public class TesterClass {
​
    public static void main(final String[] args) {
        final PublishSubject<Void> publishSubject = PublishSubject.create();
​
        publishSubject
                .retry()
                .subscribe(
                        aVoid -> System.out.println("onNext"),
                        Throwable::printStackTrace,
                        () -> System.out.println("onComplete")
                );
​
        publishSubject.onNext(null);
        publishSubject.onNext(null);
        publishSubject.onError(new Exception("onError"));
        publishSubject.onNext(null);
    }
}

My ideal use case would allow me to subscribe to all errors and all next calls from the topic/observable and take action.

I also tried implementing the solution with a custom Operator as shown in the image but I had no luck either.

Is it possible to implement what I intend to do, or does RxJava’s onError break design completely block the idea.

Solution

As described in this post (Rxjava discussion), this cannot be done.

If the error

should not terminate the stream, the error is wrapped into the message via OnNext.

Related Problems and Solutions