使用 Retrofit 抛出异常时,堆栈跟踪不会指向 API 调用的来源

问题描述 投票:0回答:1

我正在使用 Retrofit、OkHttp、Kotlin 协程来发出 HTTP 请求。

当服务器返回不成功的结果(例如 404)时,我会抛出一个自定义异常,它是

IOException
的子类型。
结果堆栈跟踪不指向最初发出请求的代码。
生成的堆栈跟踪仅指向拦截器代码,这不能帮助我识别原始调用站点。

问题:

如何确保当我抛出自定义异常(

IOException
的子类型)时,堆栈跟踪指向最初发出请求的代码(例如
catApi.callThatReturns404()
)?

限制:

  • 用 try/catch 包装每个远程 API 调用不是一个选项,因为问题是全局性的,我不想将其包装在数百个地方。
  • 仅在拦截器内进行日志记录是不够的,因为我需要完整的堆栈跟踪。
  • 避免使用自定义异常不是一个选项,因为它包含信息。
  • 解决方案也应该适用于生产代码。不仅是某些调试工具打开时

示例代码

fun main(): Unit = runBlocking {
    val api = createCatApi()
    api.callThatReturns404()
}

private fun createCatApi(): CatApi {
    val url = "https://cat-fact.herokuapp.com/"
    val httpClient = OkHttpClient.Builder()
        .addInterceptor { chain ->
            val response = chain.proceed(chain.request())
            if (!response.isSuccessful) {
                throw SubTypeOfIOException(response)
            }
            response
        }
        .build()
    val retrofit = Retrofit.Builder()
        .baseUrl(url)
        .addConverterFactory(JacksonConverterFactory.create(ObjectMapper().registerKotlinModule()))
        .client(httpClient)
        .build()
    return retrofit.create<CatApi>()
}

interface CatApi {
    @GET("factss")
    suspend fun callThatReturns404(): Collection<Any>
}

抛出

SubTypeOfIOException
时的堆栈跟踪

Exception in thread "main" com.SubTypeOfIOException: 404
    at com.MainKt$createCatApi$$inlined$-addInterceptor$1.intercept(OkHttpClient.kt:1082)

我想要什么

Exception in thread "main" com.retrofit_test.SubTypeOfIOException: 404
    at com.retrofit_test.Main.main(Main.kt:15)
Caused by: com.SubTypeOfIOException: 404
    at com.MainKt$createCatApi$$inlined$-addInterceptor$1.intercept(OkHttpClient.kt:1082)

有趣的事情:当我从测试中调用此类代码并抛出

IOException
时,堆栈跟踪包含我想要的信息。如果在测试中我抛出
IOException
的子类型,则堆栈跟踪不包含此类信息

版本:

  • org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0
  • com.squareup.retrofit2:改造:2.11.0
  • com.squareup.okhttp3:okhttp:4.12.0
kotlin retrofit kotlin-coroutines okhttp stack-trace
1个回答
0
投票

如果缺少框架与协程有关,https://github.com/Anamorphosee/stacktrace-decoroutinator可能会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.