Prevent OnErrorNotImplementedException

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

 .onErrorResumeNext(Observable.empty()) 

This OnErrorNotImplementedException not thrown, however, if I pass the second action as well, the action is never thrown.

Secondly:

 .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 is if observeOn() is called later, then it will be asynchronous, and obviously my exception handling will not work here.

Is there any way to achieve this. I would like to have a subscribe() method that does not throw an OnErrorNotImplementedException into an onError .

+5
source share
3 answers

this is how we do it at work. Instead of doing something, we made an abstraction of NYTSubscriber that implemented onError and onCompleted. Thus, you can use this subscriber and only implement the onNext callback or you can override onError and onCompleted if necessary

 public abstract class NYTSubscriber<T> extends Subscriber<T> { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } } 
+8
source

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

 .subscribe(t -> doSomething(t), e -> showError(e)); 
+8
source

When you do it like this: .onErrorResumeNext (Observable.empty ()), your thread will terminate when an error occurs - why your actions are never called. You can use '.retry ()' and your thread will restart automatically if you have an error.

0
source

Source: https://habr.com/ru/post/1235308/


All Articles