1

我正在尝试捕获自定义异常,在我的情况下它是 UserUnAuthorizedException,我正在尝试从 OkHttp 拦截器中抛出并在 Kotlin Flow 中捕获,并且在 catch 块中我能够捕获此异常

java.io.IOException:由于 UserUnAuthorizedException 而取消

但是应用程序崩溃了,我无法理解为什么会这样,下面是我的代码

suspend fun execute(): Flow<Resource<AccessToken>> = flow {
    emit(Resource.Loading)

    try {
        val loginData = iApi.authorizations(AuthRequestModel().generate())
        emit(Resource.Success(loginData))
    } catch (e: UserUnAuthorizedException) {
        emit(Resource.Error(e))
    }


}

OkHttp 拦截器

 val client = OkHttpClient.Builder()

    .addInterceptor { chain  ->
                val request = chain.request()
                val response = chain.proceed(request)
                when (response.code()) {
                    401 ->
                        throw UserUnAuthorizedException(app.resources.getString(R.string.invalid_credential))
                }
                response
            }
4

0 回答 0