Android – Handle Network error with Retrofit observable


When using Observables with Retrofit how do you handle Network failure?

Given this code:

Observable<GetJobResponse> observable = api.getApiService().getMyData();
    .doOnNext(new Action1<GetJobResponse>() {
        public void call(GetJobResponse getJobResponse) {
            //do stuff with my data
    .doOnError(new Action1<Throwable>() {
        public void call(Throwable throwable) {
            //do stuff with error message

The request just fails without network and onError is not called. It does not crash, but fails silently. Logs shows Retrofit getting the error: Unable to resolve host "": No address associated with hostname
     at com.squareup.okhttp.internal.Dns$1.getAllByName(

Using callbacks this is simply passed to the onFailure(RetrofitError error). How can I get this surfaced with RxJava?

Best Solution

My problem was actually elsewhere in my code. Handling network errors with rxJava + Retrofit is very easy as it just throws a RetrofitError in the onError method:

 public void onError(Throwable e) {
     if (e instanceof RetrofitError) {
        if (((RetrofitError) e).isNetworkError()) {
            //handle network error
        } else {
            //handle error message from server
Related Question