I am using Retrofit, OkHttp, Kotlin coroutines to make HTTP requests.
When the server returns an unsuccessful result (e.g., 404), I throw a custom exception, which is a subtype of IOException.
The resulted stack trace does not point to code that initially made a request.
The resulted stack trace only points to interceptor code, which doesn’t help me identify the original call site.
Question:
How can I ensure that when I throw a custom exception (a subtype of IOException), the stack trace points to code that initially made a request (e.g. catApi.callThatReturns404())?
Constraints:
Example code
fun main(): Unit = runBlocking {
makeApiCall()
}
private suspend fun makeApiCall() {
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>
}
Stack trace, when SubTypeOfIOException is thrown
Exception in thread "main" com.SubTypeOfIOException: 404
at com.MainKt$createCatApi$$inlined$-addInterceptor$1.intercept(OkHttpClient.kt:1082)
What I want
Exception in thread "main" com.retrofit_test.SubTypeOfIOException: 404
at com.retrofit_test.Main.makeApiCall(Main.kt:20)
at com.MainKt$main$1.invokeSuspend(Main.kt:15)
at com.MainKt.main(Main.kt:14)
Caused by: com.SubTypeOfIOException: 404
at com.MainKt$createCatApi$$inlined$-addInterceptor$1.intercept(OkHttpClient.kt:1082)
Interesting thing: when I call such code from tests and throw IOException, the stack trace contains information I want. If in tests I throw a subtype of IOException, the stack trace does not contain such information
Versions:
if the lack of frames is related to the coroutines, https://github.com/Anamorphosee/stacktrace-decoroutinator might help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With