When using Observables with Retrofit how do you handle Network failure?
Given this code:
Observable observable = api.getApiService(
This is purely an RxJava issue, not involving that much from Retrofit. doOnError
is a side-effect, so even though it handles the error, it does not "catch" in the sense of preventing it from bubbling forwards.
You should look at Error Handling Operators. The most basic option is onErrorReturn()
which allows you to substitute an error with an item (instance of GetJobResponse
).
You can put an "error" flag on that item in order to identify it later when you subscribe to the observable
. Also it's important to know that if you don't subscribe to that observable, it is essentially "dead". You should always subscribe to observables rather than using "doOn___" which are just side-effects (should be used for logging and other non-critical functionality).