Reputation: 25816
I am upgrading rxjava 1 to rxjava 2. There is OnErrorThrowable in rxjava 1 but not found in rxjava 2. What can I do in the following code to replace the OnErrorThrowable?
static Observable<String> sampleObservable() {
return Observable.defer(new Callable<ObservableSource<String>>() {
@Override
public ObservableSource<String> call() throws Exception {
try {
// Do some long running operation
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
} catch (InterruptedException e) {
throw OnErrorThrowable.from(e);
}
Log.d(TAG, "The sleep is over, now produce something");
return Observable.just("one", "two", "three", "four", "five");
}
});
}
Upvotes: 1
Views: 332
Reputation: 69997
In 2.x, you don't have to wrap exceptions as all functional types declare throws Exception
:
static Observable<String> sampleObservable() {
return Observable.defer(new Callable<ObservableSource<String>>() {
@Override
public ObservableSource<String> call() throws Exception {
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
Log.d(TAG, "The sleep is over, now produce something");
return Observable.just("one", "two", "three", "four", "five");
}
});
}
Upvotes: 2
Reputation: 6839
You can return Observable.error(e);
instead of throwing in your catch block.
Upvotes: 2