10

I am using onErrorReturn to emit a particular item rather than invoking onError if the observable encounters an error:

Observable<String> observable = getObservableSource(); observable.onErrorReturn(error -> "All Good!") .subscribeOn(Schedulers.io()) .observeOn(Schedulers.trampoline()) .subscribe(item -> onNextAction(), error -> onErrorAction() ); 

This works fine, but I want to consume the error in onErrorReturn only if certain conditions are met. Just like rethrowing an exception from inside a catch block.

Something like:

onErrorReturn(error -> { if (condition) { return "All Good!"; } else { // Don't consume error. What to do here? throw error; // This gives error [Unhandled Exception: java.lang.Throwable] } }); 

Is there a way to propagate the error down the observable chain from inside onErrorReturn as if onErrorReturn was never there?

1
  • Did you manage to solve this ? Commented Aug 28, 2018 at 10:39

2 Answers 2

16

Using onErrorResumeNext I guess you can achieve what you want

observable.onErrorResumeNext(error -> { if(errorOk) return Observable.just(ok) else return Observable.error(error) }) .subscribeOn(Schedulers.io()) .observeOn(Schedulers.trampoline()) .subscribe(item -> onNextAction(), error -> onErrorAction() ); 
Sign up to request clarification or add additional context in comments.

Comments

-2

You can create an method that will receive this error normally, something like

private void onError(Throwable error) { // do whatever you want with your error } ... .onErrorReturn( error -> onError(error) ) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.