Retrofit + Kotlin coroutines : get info of actual Java exception instead of just kotlin.Unit
I am using retrofit2 version 2.11.0 in Android with Kotlin. When I make an API call defined in ApiService interface inside a viewModelScope. launch { } coroutine block, and I put a try catch block to catch any exception, this always gives kotlin.Unit exception:
fun callApi() = viewModelScope.launch {
try {
val resp = apiService.getData()
} catch (e:Exception) { // this always gives kotlin.Unit exception
Log.e(TAG, "callApi: ${e.printStackTrace()}")
}
}
The catch block always receives kotlin.Unit exception and never gives info about the actual Java exception, e.g. when I turn off the internet, it should receive UnknownHostException, but it's always kotlin.Unit. Also have tried try-catching the chain.proceed() method in a custom interceptor, there as well it gives kotlin.Unit exception.
How to get the actual Java exception in Retrofit with Kotlin coroutines?
@JakeWharton , @swankjesse
Hi ,i also has kotlin.Unit issue when i use quarkus framework.
Caused by: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: Unsupported features in 2 methods Detailed message: Error: Discovered unresolved type during parsing: kotlin.Unit. This error is reported at image build time because class retrofit2.Utils is registered for linking at image build time by command line and command line. Error encountered while parsing retrofit2.HttpServiceMethod.parseAnnotations(HttpServiceMethod.java:39) Parsing context: at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:39) at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:235) at retrofit2.Retrofit.validateServiceInterface(Retrofit.java:208) at retrofit2.Retrofit.create(Retrofit.java:158) at
I am using retrofit2 version 2.11.0 in Android with Kotlin. When I make an API call defined in ApiService interface inside a viewModelScope. launch { } coroutine block, and I put a try catch block to catch any exception, this always gives kotlin.Unit exception:
fun callApi() = viewModelScope.launch {try {val resp = apiService.getData()} catch (e:Exception) { // this always gives kotlin.Unit exceptionLog.e(TAG, "callApi: ${e.printStackTrace()}")}}The catch block always receives kotlin.Unit exception and never gives info about the actual Java exception, e.g. when I turn off the internet, it should receive UnknownHostException, but it's always kotlin.Unit. Also have tried try-catching the chain.proceed() method in a custom interceptor, there as well it gives kotlin.Unit exception.How to get the actual Java exception in Retrofit with Kotlin coroutines?
@JakeWharton , @swankjesse
That's because printStackTrace() returns Unit (this is nothing to do with Retrofit). You want to call the e.stackTraceToString() method instead