相关疑难解决方法(0)

处理 Kotlin 协程中自定义 okhttp 拦截器抛出的异常

Interceptor在我的 Android 应用程序中使用自定义和 Retrofit 客户端,在某些特定情况下会引发异常。我正在尝试使用 Kotlin 协程使其工作。

问题是我无法处理前面提到的错误,因为在从 Interceptor 实例中抛出异常的那一刻,它会使整个应用程序崩溃,而不是在协程的try/catch语句中被捕获。当我使用 Rx 实现时,异常完美地传播到onError回调中,在那里我能够以我需要的方式处理它。

我想这与用于网络调用的底层线程有某种关系,请从调用的地方、在抛出异常之前的拦截器和堆栈跟踪中查看下面的日志:

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)
Run Code Online (Sandbox Code Playgroud)

我应该怎么做才能正确地从拦截器中捕获和处理这个异常?我错过了什么吗?

android exception interceptor okhttp kotlin-coroutines

18
推荐指数
2
解决办法
6203
查看次数