4gus71n
4gus71n

Reputation: 4111

UnknownHostException and ConnectionException being fired by Retrofit

Im using Retrofit with the RxJava adapter set in my Android App. The issue is that I got some reports that in some devices Retrofit is firing ConnectionException and in some other devices is firing UnknownHostException. This makes pretty difficult to plug other rx.Observables using the onErrorResume() method. Even worse, the only common parent that both Exceptions share is IOException which is pretty generic. There's a way to unify this?

I was only handling the UnknownHostException case. But now I also have this ConnectionException, and who knows which other exception in some other device.

I could map this two exceptions to a generic custom exception, something like NoInternetConnectionException. But, there's more of this exceptions out there?

UPDATE:

Currently my Interactors/UseCases are doing things like:

mNetworkUserRepository.getUserList()
    .onErrorResule(ObservableUtils.whenExceptionIs(RetrofitError.class), mUserDatabase.getUserList()) //Server side error
    .onErrorResule(ObservableUtils.whenExceptionIs(UnknownHostException.class), mUserDatabase.getUserList()) //No internet
    .onErrorResule(ObservableUtils.whenExceptionIs(ConnectionException.class), mUserDatabase.getUserList()) //ANOTHER EXCEPTION FOR THE SAME THING
    .subscribe(new MySubscriber<List<Users>>() {
        @Override
        public void onNext(List<Users> response) {
            callback.showOnUI(response);
        }
    });

So basically in Samsung S7 the UnkownHostException is being fired, in another device a Nexus, ConnectionException is being fired. I don't like this, how many other "no internet" exceptions are out there? I though that Retrofit only fired UnknownHostException if I tried to make an http request without internet connection. How can I group this? I don't wanna attach N onResumeErrors for different devices and stuff

Upvotes: 3

Views: 2078

Answers (1)

yosriz
yosriz

Reputation: 10267

You can't actually know from an exception whether there is no internet, or some other network error occurred along the way, as you encountered, there are many others situations and Exceptions you might get like SocketTimeoutException, UnknowHostException or any other network related Exception derived from IOException (you can see the dozens of derived classes from IOException), it may or may not be related to no internet, as network operation can fail in so many phases from different sources, especially when you on mobile network.

So, I think that if you really need 'no internet' behavior you should check it before the request and act differently ahead.

When you do perform a call, I think there are 2 kinds of failure to consider, IOException for any network error happened along the way. (not much to do but retry/use cache whatever your case), and the retrofit error (HttpException) that means you got response from Server (no network error) but something wrong with the request itself (there you can have plenty of reasons for error you might act upon - authentication error (token), authorization etc.)

Upvotes: 2

Related Questions