question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handling Network Error in Rxjava 2 - Retrofit 2

See original GitHub issue

How can we handle different network errors in Rxjava2 ?

We used to check the instance of the throwable if it’s of IOException or HttpException back with Rxjava 1 ,however, in RxJava 2 the throwable error is of type GaiException.

code snippet

 RestAPI restAPI = RetrofitHelper.createRetrofitWithGson().create(RestAPI.class);

        Observable<BaseResponseTourPhoto> observable = restAPI.fetchData("Bearer " + getAccessToken(), "2", "" + page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());

        Disposable subscription = observable.subscribe(BaseResponse-> {
            onLoadingFinish(isPageLoading, isRefreshing);
            onLoadingSuccess(isPageLoading, BaseResponse);
            writeToRealm(BaseResponse.getData());
        }, error -> {
            onLoadingFinish(isPageLoading, isRefreshing);
            onLoadingFailed(error);
        });

        mCompositeDisposable = new CompositeDisposable();
        mCompositeDisposable.add(subscription);

        unsubscribeOnDestroy(mCompositeDisposable);

   private void onLoadingFailed(Throwable error) {
        try {
            // We had non-200 http error
            if (error instanceof HttpException) {
                HttpException httpException = (HttpException) error;
                Response response = httpException.response();
                Log.i(TAG, error.getMessage() + " / " + error.getClass());
            }
            // A network error happened
            if (error instanceof IOException) {
                Log.i(TAG, error.getMessage() + " / " + error.getClass());
            }

            Log.i(TAG, error.getMessage() + " / " + error.getClass());
        } catch (Exception e) {
            Log.i(TAG, e.getMessage());
        }
    }```


reference: https://github.com/square/retrofit/issues/690

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:23 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
eGoretscommented, Jun 8, 2017

@JakeWharton the same issue There is my list of dependencies:

compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'

compile 'com.squareup.okhttp3:okhttp:3.8.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'

compile 'com.trello.rxlifecycle2:rxlifecycle:2.0.1'
compile 'com.trello.rxlifecycle2:rxlifecycle-components:2.0.1'`

But my main issue is – when start internet request with no internet connection, I can’t handle this error and app crashes.

06-08 11:46:16.670 io.cex.app.dev.debug W/System.err: java.net.UnknownHostException: Unable to resolve host "dev.chain.com": No address associated with hostname
06-08 11:46:16.690 io.cex.app.dev.debug W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:457)
06-08 11:46:16.690 io.cex.app.dev.debug W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
06-08 11:46:16.690 io.cex.app.dev.debug W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:215)
06-08 11:46:16.690 io.cex.app.dev.debug W/System.err:     at okhttp3.Dns$1.lookup(Dns.java:39)
06-08 11:46:16.690 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:171)
06-08 11:46:16.690 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:137)
06-08 11:46:16.690 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:82)
06-08 11:46:16.690 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:171)
06-08 11:46:16.690 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121)
06-08 11:46:16.690 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100)
06-08 11:46:16.691 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
06-08 11:46:16.691 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
06-08 11:46:16.691 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
06-08 11:46:16.691 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
06-08 11:46:16.691 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
06-08 11:46:16.691 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
06-08 11:46:16.692 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
06-08 11:46:16.692 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
06-08 11:46:16.692 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
06-08 11:46:16.692 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
06-08 11:46:16.692 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
06-08 11:46:16.692 io.cex.app.dev.debug W/System.err:     at com.ihsanbal.logging.LoggingInterceptor.intercept(LoggingInterceptor.java:56)
06-08 11:46:16.692 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
06-08 11:46:16.692 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
06-08 11:46:16.692 io.cex.app.dev.debug W/System.err:     at com.readystatesoftware.chuck.ChuckInterceptor.intercept(ChuckInterceptor.java:172)
06-08 11:46:16.692 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
06-08 11:46:16.692 io.cex.app.dev.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
06-08 11:46:16.692 io.cex.app.dev.debug W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
06-08 11:46:16.692 io.cex.app.dev.debug W/System.err:     at okhttp3.RealCall.execute(RealCall.java:69)
06-08 11:46:16.692 io.cex.app.dev.debug W/System.err:     at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at com.jakewharton.retrofit2.adapter.rxjava2.CallObservable.subscribeActual(CallObservable.java:41)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10179)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at com.jakewharton.retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10179)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at io.reactivex.internal.operators.observable.ObservableMap.subscribeActual(ObservableMap.java:32)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10179)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at io.reactivex.internal.operators.observable.ObservableDoOnEach.subscribeActual(ObservableDoOnEach.java:42)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10179)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at io.reactivex.internal.operators.observable.ObservableDoOnEach.subscribeActual(ObservableDoOnEach.java:42)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10179)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at io.reactivex.internal.operators.observable.ObservableSubscribeOn$1.run(ObservableSubscribeOn.java:39)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
06-08 11:46:16.693 io.cex.app.dev.debug W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-08 11:46:16.694 io.cex.app.dev.debug W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-08 11:46:16.694 io.cex.app.dev.debug W/System.err:     at java.lang.Thread.run(Thread.java:818)
06-08 11:46:16.694 io.cex.app.dev.debug W/System.err: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
06-08 11:46:16.696 io.cex.app.dev.debug W/System.err:     at libcore.io.Posix.android_getaddrinfo(Native Method)
06-08 11:46:16.696 io.cex.app.dev.debug W/System.err:     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
06-08 11:46:16.696 io.cex.app.dev.debug W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
06-08 11:46:16.696 io.cex.app.dev.debug W/System.err: 	... 47 more
06-08 11:46:17.536 io.cex.app.dev.debug E/AndroidRuntime: FATAL EXCEPTION: main
                                                          Process: io.cex.app.dev.debug, PID: 4419
6reactions
lucas34commented, Feb 2, 2017

You can use ‘com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0’

Read more comments on GitHub >

github_iconTop Results From Across the Web

Retrofit 2— Best way for Handling Network Error - Medium
Retrofit 2 — Best way for Handling Network Error. Getting bored to check internet connectivity before each network call again and again ?...
Read more >
Handling Network Error in Rxjava 2 - Retrofit 2 - Stack Overflow
How can we handle different network errors in Rxjava2 ? We used to check the instance of the throwable if it's of IOException...
Read more >
Legacy Project Refactoring: Handling API Errors with Retrofit2 ...
Conclusion. Handling API errors with Retrofit2 and RxJava is a great solution for a few reasons. First, you get rid of excessive, repeating...
Read more >
Centralized Network Error Handling Retrofit - AndroidWave
We'll learn centralized network error handling in Android. ... 2. Do Retrofit configuration with OkHttp. Go to the network folder in source ...
Read more >
Custom Error Handling with RxJava & Retrofit 2 - Dan Stone
We've made our own subclass of Exception that holds the message and cause. This form of Exception is called when there's an internet...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found