JakeWharton/retrofit2-kotlin-coroutines-adapter

Can't catch exceptions

Closed this issue · 2 comments

I'm trying to do something like:

class Presenter{
	fun doStuff(){
		myUiContext.launch{
			val data = withContext(backgroundContext){
				datasource.getSomeData()
			}
		}
	}
}

class Datasource{
	suspend fun getSomeData:Result<Data,Error>{
		return try{
			val data = retrofitService.getSomeDataAsync().await()
			Success(data)
		}catch(t:Throwable){
			Failure(mapToError(t))
		}
	}
}


class RetrofitService{
    @GET(Endpoint.SOME_DATA)
    fun getSomeDataAsync(): Deferred<Data>
}

And I've got Fatal exception:

E/AndroidRuntime: FATAL EXCEPTION:
retrofit2.HttpException: HTTP 404 Not Found
at com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory$BodyCallAdapter$adapt$2.onResponse(CoroutineCallAdapterFactory.kt:104)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:129)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:216)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)

Why exception is not handled by try catch? What I do wrong?

updating to 1.1.1 version of coroutines is fixed the issue

Hello @freaksgit , i came into your problem, but i was wondering if is there any solution to catch the exception within the retrofitService or any other way to catch error just once in all my code.

This way it's forcing me to call try/catch for each network call.