RxJava: Handling Network Error in Rxjava 2 - Retrofit 2

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

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 23 (4 by maintainers)

Most upvoted comments

@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

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

Use 2.3.0

On Thu, Jun 8, 2017 at 11:53 AM eGorets notifications@github.com wrote:

@ianomad https://github.com/ianomad Error:Could not find com.jakewharton.retrofit:retrofit2-rxjava2-adapter:2.2.0. Was it deployed into repo?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/ReactiveX/RxJava/issues/4942#issuecomment-307146355, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEdYQA9y18kuiKPM31EJQZKd9NU-Eks5sCBjzgaJpZM4LXi_I .

Add this statement to Application class

RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        });

@JakeWharton the same issue

 
 "rxjava"                   : "io.reactivex.rxjava2:rxjava:2.0.5",
 "rxandroid"                : "io.reactivex.rxjava2:rxandroid:2.0.1",
  converter-gson"           : "com.squareup.retrofit2:converter-gson:2.1.0",
  "retrofit2-rxjava2-adapter": "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0",

no internet connection, repeated requests with exception

java.io.IOException: java.net.UnknownHostException: Unable to resolve host "url": No address associated with hostname
    at com.xxx.core.http.interceptor.strategy.RequestStrategy.request(RequestStrategy.java:53)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at com.xxx.core.http.interceptor.CacheInterceptor.intercept(CacheInterceptor.java:57)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at okhttp3.RealCall.execute(RealCall.java:69)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at com.jakewharton.retrofit2.adapter.rxjava2.CallObservable.subscribeActual(CallObservable.java:41)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at io.reactivex.Observable.subscribe(Observable.java:10685)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at io.reactivex.internal.operators.observable.ObservableSubscribeOn$1.run(ObservableSubscribeOn.java:39)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at io.reactivex.Scheduler$1.run(Scheduler.java:134)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:59)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:51)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
            06-09 13:00:43.786 6602-8305/com.xxx E/CrashReport: ║ 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
            06-09 13:00:43.787 6602-8305/com.xxx E/CrashReport: ║ 	at java.lang.Thread.run(Thread.java:761)

I can’t handle this error and app crashes.