Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch an exception that has been thrown inside Retrofit Interceptor? #34

Closed
melanke opened this issue Dec 18, 2017 · 2 comments
Closed

Comments

@melanke
Copy link

melanke commented Dec 18, 2017

Inside my retrofit interceptor I am throwing a RespException (A custom RuntimeException) like this:

val interceptor = Interceptor {
    chain ->

    val request = chain
            .request()
            .newBuilder()
            .build()

    // <other irrelevant things here>

    var resp: Response

    try {

        resp = chain.proceed(request)

    } catch (e: Exception) {
        val errorTxt = "Unexpected error"

        // <other irrelevant things here>

        var re = RespException(errorTxt)
        re.initCause(e)
        throw re
    }

    // <other irrelevant things here>

    resp
}

But when I try to catch it like this:

fun load() = async(UI) {
    // <other irrelevant things here>

    try {
        var resp = Api.resources.listPrincipal(null, null, null, null, null).await()

        // <other irrelevant things here>

    } catch (e: RespException){
        // it's not called
    } catch (t: Throwable){
        // it's not called
    }

    // <other irrelevant things here>
}

It doesn't work. It crashes my app with this stacktrace (I turned off the server purpose, just to get the error):

E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: br.com.martinlabs.usecase, PID: 9587
com.simpli.model.RespException: Unexpected Error
    at br.com.martinlabs.usecase.service.Api$Companion$interceptor$1.intercept(Api.kt:51)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    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.ConnectException: Failed to connect to /192.168.30.194:8080
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:225)
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:149)
    at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:192)
    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121)
    at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at br.com.martinlabs.usecase.service.Api$Companion$interceptor$1.intercept(Api.kt:45)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185) 
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135) 
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
    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.ConnectException: failed to connect to /192.168.30.194 (port 8080) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
    at libcore.io.IoBridge.isConnected(IoBridge.java:234)
    at libcore.io.IoBridge.connectErrno(IoBridge.java:171)
    at libcore.io.IoBridge.connect(IoBridge.java:122)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
    at java.net.Socket.connect(Socket.java:884)
    at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:63)
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:223)
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:149) 
    at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:192) 
    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121) 
    at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100) 
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
    at br.com.martinlabs.usecase.service.Api$Companion$interceptor$1.intercept(Api.kt:45) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185) 
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135) 
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
    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: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
    at libcore.io.IoBridge.isConnected(IoBridge.java:223)
    at libcore.io.IoBridge.connectErrno(IoBridge.java:171) 
    at libcore.io.IoBridge.connect(IoBridge.java:122) 
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183) 
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452) 
    at java.net.Socket.connect(Socket.java:884) 
    at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:63) 
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:223) 
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:149) 
    at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:192) 
    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121) 
    at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100) 
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
    at br.com.martinlabs.usecase.service.Api$Companion$interceptor$1.intercept(Api.kt:45) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185) 
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135) 
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
    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) 
@gildor
Copy link
Owner

gildor commented Dec 18, 2017

Interceptor handles only IOExceptions. So I suppose your RespException should extend IOException to be propagated by OkHttp.
And doesn't see how such case differs from use of OkHttp and Retrofit without coroutines, because according to your stack trace your app crashes before coroutines or even Retrofit code

@melanke
Copy link
Author

melanke commented Dec 18, 2017

Thank you very much! That is the problem, RespException should inherit IOException.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants