当我在拦截器中抛出异常时,Retrofit会崩溃

Dja*_*gow 12 android kotlin retrofit2 okhttp3

我有一个处理一些身份验证的拦截器.如果身份验证失败,则抛出和异常.根据我可以发现抛出异常应该导致onFailure在我处理异常的地方被调用.不幸的是,这不会发生,应用程序完全崩溃.我相信我一定会错过一些东西,但我似乎无法弄明白.希望有人在这里可以帮助:)

代码和堆栈跟踪如下:

val client = OkHttpClient.Builder()
     // Add interceptor that throws
     .addInterceptor { chain ->
         throw Exception("test")
     }
     .build()

retrofit = Retrofit.Builder()
                .baseUrl(baseURL)
                .client(client)
                .build()

api = retrofit.create(ApiInterface::class.java)


// Create api call...

apicall.enqueue(object : Callback<T> {
    override fun onResponse(call: Call<T>?, response: retrofit2.Response<T>?) {
        // ....
    }

    override fun onFailure(call: Call<T>?, t: Throwable?) {
        // Expect to go here to handle the Exception. But the app crashes
    }
})
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
                  Process: com.testapp.test.debug, PID: 28891
                  java.lang.Error: java.lang.Exception: test
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1168)
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                      at java.lang.Thread.run(Thread.java:764)
                   Caused by: java.lang.Exception: test
                      at com.testapp.test.data.networking.RetrofitWebApi$client$1.intercept(RetrofitWebApi.kt:90)
                      at com.testapp.test.data.networking.RetrofitWebApi$client$1.intercept(RetrofitWebApi.kt:76)
                      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:1162)
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
                      at java.lang.Thread.run(Thread.java:764) 
Run Code Online (Sandbox Code Playgroud)

zsm*_*b13 20

OkHttp只会捕获Interceptorif中的异常IOException.您可以在此处查看执行此操作的代码,相关部分(简化)如下所示:

try {
  Response response = getResponseWithInterceptorChain();
} catch (IOException e) {
  responseCallback.onFailure(RealCall.this, e);
}
Run Code Online (Sandbox Code Playgroud)

因此,如果您将代码更改为以下内容,您onFailure将按预期在方法中获得回调:

val client = OkHttpClient.Builder()
     // Add interceptor that throws
     .addInterceptor { chain ->
         throw IOException("test")
     }
     .build()
Run Code Online (Sandbox Code Playgroud)