12

I want to achieve that if i call the Obervable.subscribe(Action1) method, it does not throw OnErrorNotImplementedException anywhere, but if i call Obervable.subscribe(Action1, Action1), the second action is called when an error is raised as normal. I tried two ways:

.onErrorResumeNext(Observable.empty())

This way OnErrorNotImplementedException is not thrown, however if i pass also the second action, the action is never called either.

Second:

.lift(new Observable.Operator<T, T>() {
    @Override
    public Subscriber<? super T> call(Subscriber<? super T> subscriber) {
        return new Subscriber<T>() {
            @Override
            public void onCompleted() {
                if (!subscriber.isUnsubscribed()) {
                    subscriber.onCompleted();
                }
            }

            @Override
            public void onError(Throwable e) {
                if (!subscriber.isUnsubscribed()) {
                    try {
                        subscriber.onError(e);
                    } catch (Throwable t) {
                        if (!(t instanceof OnErrorNotImplementedException)) {
                            throw t;
                        }
                    }
                }
            }

            @Override
            public void onNext(T t) {
                if (!isUnsubscribed()) {
                    subscriber.onNext(t);
                }
            }
        };
    }
});

The problem with this if observeOn() is called later then this will be asynchronous and obviously my exception handling here will not work.

Is there way to achieve this. I wish there would be a subscribe() method which does not throw OnErrorNotImplementedException in onError.

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
WonderCsabo
  • 11,947
  • 13
  • 63
  • 105

5 Answers5

16

Here is another possible solution, you can define the onNext and a Throwable (also you cannot loose the lambda syntax):

.subscribe(t -> doSomething(t), e -> showError(e));
FireZenk
  • 1,056
  • 1
  • 16
  • 28
  • 2
    Thanks! Well, my intention was not adding a dummy onError everywhere. But since there is no other solution, I do now. – WonderCsabo May 30 '16 at 17:05
10

here's how we do it at work. Instead of making actions we made an abstract NYTSubscriber which has onError and onCompleted implemented. This way you can use this subscriber and only implement the onNext callback OR you can override onError and onCompleted when necessary

public abstract class NYTSubscriber<T> extends Subscriber<T> {

@Override
public void onCompleted() {
}

@Override
public void onError(Throwable e) {
}
}
FriendlyMikhail
  • 2,857
  • 23
  • 39
  • 1
    Thanks! Yeah, this is a way. My problem is that in this way i loose the lambda syntax in the subscriber and i always have to add anonymous classes. :( – WonderCsabo Nov 05 '15 at 17:09
  • Apparently there is no other way than passing an empty `onError()` implementation, either with lambda or subclass. So i am accepting this answer. – WonderCsabo Nov 06 '15 at 08:31
  • Check my solution below @WonderCsabo – FireZenk May 30 '16 at 14:15
5

If you are using Kotlin then the syntax is like

 .subscribe({success -> doOnSuccess(success)},{error -> doOnError(error)})
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
0

When you do like this '.onErrorResumeNext(Observable.empty())' your stream will be completed when error occurs - that's why your actions is never called. You can user '.retry()' and your stream will be restarted automatically when you have an error.

0

This Error comes when calling the Subscribe method, but not providing onError() callbacks. Kotlin

subscribe(object : DisposableCompletableObserver() {
    override fun onComplete() {
        // on successful completion   }
    override fun onError(e: Throwable) {
        //error message
    }
    }
  )