Prevent OnErrorNotImplementedException
Asked Answered
M

5

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.

Malicious answered 5/11, 2015 at 15:30 Comment(0)
B
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) {
}
}
Battologize answered 5/11, 2015 at 16:32 Comment(3)
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. :(Malicious
Apparently there is no other way than passing an empty onError() implementation, either with lambda or subclass. So i am accepting this answer.Malicious
Check my solution below @MaliciousGt
G
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));
Gt answered 30/5, 2016 at 14:15 Comment(1)
Thanks! Well, my intention was not adding a dummy onError everywhere. But since there is no other solution, I do now.Malicious
B
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) {
}
}
Battologize answered 5/11, 2015 at 16:32 Comment(3)
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. :(Malicious
Apparently there is no other way than passing an empty onError() implementation, either with lambda or subclass. So i am accepting this answer.Malicious
Check my solution below @MaliciousGt
P
5

If you are using Kotlin then the syntax is like

 .subscribe({success -> doOnSuccess(success)},{error -> doOnError(error)})
Prejudice answered 26/11, 2019 at 9:57 Comment(0)
N
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.

Nanice answered 9/8, 2017 at 8:30 Comment(0)
D
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
    }
    }
  )
Domeniga answered 19/3, 2020 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.