Handle exceptions thrown by a custom okhttp Interceptor in Kotlin Coroutines
我在Android应用程序中将自定义
问题是我无法处理前面提到的错误,因为从Interceptor实例中引发异常的那一刻起,它使整个应用程序崩溃,而不是被协程的
我猜想这与网络调用所使用的基础线程有某种关系,请在引发异常之前从进行调用的位置,拦截器以及堆栈跟踪中查看以下日志:
1 2 3 4 5 6 7 | 2019-11-04 17:17:34.515 29549-29729/com.app W/TAG: Running thread: DefaultDispatcher-worker-1 2019-11-04 17:17:45.911 29549-29834/com.app W/TAG: Interceptor thread: OkHttp https://some.endpoint.com/... 2019-11-04 17:17:45.917 29549-29834/com.app E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher Process: com.app, PID: 29549 com.app.IllegalStateException: Passed refresh token can\'t be used for refreshing the token. at com.app.net.AuthInterceptor.intercept(AuthInterceptor.kt:33) |
为了能够正确地从拦截器捕获并处理此异常,我应该怎么做? 我想念什么吗?
您应该子类化
我们认为像
我不知道您到底需要什么,但是这样理解:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException { Request request = chain.request(); okhttp3.Response response = chain.proceed(request); // todo deal with the issues the way you need to if (response.code() == SomeCode) { //do something return response; } return response; } }) .build(); Retrofit.Builder builder = new Retrofit.Builder() .baseUrl(url) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create()); Retrofit retrofit = builder.build(); |