android – Handle Network error with Retrofit observable-ThrowExceptions

Exception or error:

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

Given this code:

Observable<GetJobResponse> observable = api.getApiService().getMyData();
observable
    .doOnNext(new Action1<GetJobResponse>() {
        @Override
        public void call(GetJobResponse getJobResponse) {
            //do stuff with my data
        }
    })
    .doOnError(new Action1<Throwable>() {
        @Override
        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:

 java.net.UnknownHostException: Unable to resolve host "api-staging.sittercity.com": No address associated with hostname
     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
     at java.net.InetAddress.getAllByName(InetAddress.java:214)
     at com.squareup.okhttp.internal.Dns$1.getAllByName(Dns.java:29)

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

How to solve:

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:

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

###

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).

###

Note that if you let an observable emit onError, all upstream observables will shut down – regardless of whether you “catch” it with onErrorReturn or onErrorResumeNext. Often you don’t want that. For example, if your stream starts with a button click that triggers a retrofit request which fails, then the button clicks won’t be recognized anymore since the stream terminated.

Use Observable<Response<Type>> or Observable<Result<Type>> to also handle retrofit errors in onNext().

s. http://blog.danlew.net/2015/12/08/error-handling-in-rxjava/

###

As of Retrofit2 and RxJava2 there is no more RetrofitError exception. And its successor HttpException only represents HTTP error response codes. Network errors should be handled through IOException.

 @Override
 public void onError(Throwable e) {
     if (e instanceof IOException) {
            //handle network error
     } else if (e instanceof HttpException) {
            //handle HTTP error response code
     } else {
            //handle other exceptions
     } 
 }

Leave a Reply

Your email address will not be published. Required fields are marked *