Okhttp: OnErrorNotImplementedException when Interceptor throws.

Created on 18 Mar 2016  路  10Comments  路  Source: square/okhttp

I get this exception any time there's SocketTimeoutException.
Using Retrofit2, RxJava 1.1.0, OkHttp3.

FATAL EXCEPTION: RxCachedThreadScheduler-1
                                                                   Process: com.***, PID: 13298
                                                                   java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.
                                                                       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:60)
                                                                       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
                                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
                                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                       at java.lang.Thread.run(Thread.java:818)
                                                                    Caused by: rx.exceptions.OnErrorNotImplementedException
                                                                       at rx.Observable$27.onError(Observable.java:8139)
                                                                       at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:157)
                                                                       at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
                                                                       at rx.internal.operators.OperatorSubscribeOn$1$1.onError(OperatorSubscribeOn.java:59)
                                                                       at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:151)
                                                                       at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:125)
                                                                       at rx.Observable.unsafeSubscribe(Observable.java:8314)
                                                                       at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
                                                                       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
                                                                       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)聽
                                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:237)聽
                                                                       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)聽
                                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)聽
                                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)聽
                                                                       at java.lang.Thread.run(Thread.java:818)聽
                                                                    Caused by: java.net.SocketTimeoutException
                                                                       at java.net.PlainSocketImpl.read(PlainSocketImpl.java:484)
                                                                       at java.net.PlainSocketImpl.-wrap0(PlainSocketImpl.java)
                                                                       at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
                                                                       at okio.Okio$2.read(Okio.java:139)
                                                                       at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
                                                                       at okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
                                                                       at okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
                                                                       at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
                                                                       at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:184)
                                                                       at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125)
                                                                       at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:723)
                                                                       at okhttp3.internal.http.HttpEngine.access$200(HttpEngine.java:81)
                                                                       at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:708)
                                                                       at com.***.network.interceptor.HeaderInterceptor.intercept(HeaderInterceptor.java:24)
                                                                       at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:681)
                                                                       at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:563)
                                                                       at okhttp3.RealCall.getResponse(RealCall.java:241)
                                                                       at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
                                                                       at com.***.network.interceptor.TokenExpiredInterceptor.intercept(TokenExpiredInterceptor.java:40)
                                                                       at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
                                                                       at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                                       at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
                                                                       at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
                                                                       at okhttp3.RealCall.execute(RealCall.java:57)
                                                                       at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
                                                                       at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:144)
                                                                       at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:125)聽
                                                                       at rx.Observable.unsafeSubscribe(Observable.java:8314)聽
                                                                       at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)聽
                                                                       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)聽
                                                                       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)聽
                                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:237)聽
                                                                       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)聽
                                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)聽
                                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)聽
                                                                       at java.lang.Thread.run(Thread.java:818)聽

Most helpful comment

RxLint for the rescue!

All 10 comments

Use Observer or Subscriber or one of the subscribe overloads that takes an Action1 for Throwable to add error handling to your call chain. This isn't an OkHttp issue.

Inside interceptor? There's no Rx stuff there. Why?
Outside of iterceptors every retrofit call is supplied with onError(throwable).

The stack trace indicates otherwise and RxJava corroborates:

screen shot 2016-03-18 at 12 12 17 pm

how to solve it?

RxLint for the rescue!

Thank you,I'll try it

I catched the exception,and resolved the problem:
try {
response = chain.proceed(request);
} catch (IOException e) {
LogUtil.error(TAG,"ERROR",e);
return null;
}

I've had the same problem. Unfortunately catching and returning null as Xuzhiwei proposed did not help me. Getting NullReferenceException from Observable on the other hand.

Always get carsh android app throwing the exception
"java.lang.NoClassDefFoundError: Failed resolution of: Lokhttp3/MediaType"

Still get OnErrorNotImplementedException and SocketTimeoutException and app crashed . My code:

 observable.compose(TransformerHelper.<Response<T>>io_main()).doOnSubscribe(new 
 Consumer<Disposable>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull Disposable disposable) throws Exception {
                requestCallback.onBefore();
            }
        }).doOnError(new Consumer<Throwable>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                requestCallback.onAfter();
                requestCallback.onError(throwable);
            }
        }).doOnComplete(new Action() {
            @Override
            public void run() throws Exception {
                requestCallback.onAfter();
            }
        }).subscribe(new Consumer<Response<T>>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull Response<T> response) throws Exception {
                try {
                    requestCallback.onAfter();
                    if (response.isSuccess()) {
                        requestCallback.onSuccess(response.data, response.msg);
                    } else {
                        requestCallback.onBizErr(response.code, response.msg);
                    }
                } catch (Exception e) {
                    requestCallback.onAfter();
                    requestCallback.onError(e.getCause());
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                requestCallback.onAfter();
                requestCallback.onError(throwable);
            }
        });

Log

io.reactivex.exceptions.OnErrorNotImplementedException: failed to connect to /192.168.20.122 (port 8888) after 10000ms
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.observers.LambdaObserver.onError(LambdaObserver.java:74)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onError(ObservableDoOnEach.java:119)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.observers.DisposableLambdaObserver.onError(DisposableLambdaObserver.java:64)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.checkTerminated(ObservableObserveOn.java:276)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:172)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at android.os.Looper.loop(Looper.java:135)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5314)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err: Caused by: java.net.SocketTimeoutException: failed to connect to /192.168.20.122 (port 8888) after 10000ms
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:169)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at java.net.Socket.connect(Socket.java:882)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:69)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:238)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.connection.RealConnection.connectTunnel(RealConnection.java:210)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:152)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:256)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at com.tiemagolf.api.GolfHeaderInterceptor.intercept(GolfHeaderInterceptor.java:44)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.RealCall.execute(RealCall.java:77)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10903)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10903)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at java.lang.Thread.run(Thread.java:818)
Was this page helpful?
0 / 5 - 0 ratings