JakeWharton / retrofit2-kotlin-coroutines-adapter

A Retrofit 2 adapter for Kotlin coroutine's Deferred type.
Apache License 2.0
1.97k stars 129 forks source link

ClassCastException: kotlin.Result$Failure cannot be cast to retrofit2.Response #45

Open Jeff11 opened 5 years ago

Jeff11 commented 5 years ago

Is there a way to handle exceptions from Retrofit? We often get these ClassCastExceptions in the await() call.

interface Api {
  @GET("api/config")
  fun getConfigAsync(): Deferred<Response<String>>
}
launch { // UI
  try {
    val response = api.getConfigAsync().await()
    doSomething(response)
  } catch (e: Exception) {
    Timber.e(e)
  }
}
Exception: java.lang.ClassCastException: kotlin.Result$Failure cannot be cast to retrofit2.Response
  at com.example.Setup$loadConfig$1.invokeSuspend(Setup.kt:385)
  at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
  at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:285)
  at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
  at kotlinx.coroutines.scheduling.CoroutineScheduler.access$getSchedulerName$p(CoroutineScheduler.kt:60)
  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
mikhailmelnik commented 5 years ago

Same here. Have number of similar stacktraces from every place I use coroutines in but it is impossible to find out what the root cause was. I can only guess it might be a network issue.