我有一个处理一些身份验证的拦截器。如果身份验证失败,则抛出和异常。根据我可以发现抛出异常应该导致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
}
})
堆栈追踪:
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)
答案 0 :(得分:12)
OkHttp只会捕获Interceptor
中发生的异常,如果它是IOException
。您可以看到执行此操作的代码here,相关部分(简化)如下所示:
try {
Response response = getResponseWithInterceptorChain();
} catch (IOException e) {
responseCallback.onFailure(RealCall.this, e);
}
因此,如果您将代码更改为以下内容,您将按照预期在onFailure
方法中获得回调:
val client = OkHttpClient.Builder()
// Add interceptor that throws
.addInterceptor { chain ->
throw IOException("test")
}
.build()