JakeWharton/retrofit2-kotlin-coroutines-adapter

Why do you use call.enqueue() instead of call.execute() in CallAdapter?

VolodyaVechirko opened this issue · 3 comments

@JakeWharton As I understand call.enqueue() dispatches request calling to retrofit internal mechanism (it works async and we get callbacks). But it is a coroutines-adapter and coroutines are an async framework itself. It looks like one async framework use another one. Can we just use call.execute() and return Deferred (but of course keep in mind to launch it in BG dispatcher)?

Also there is a problem to handle errors for example occurred in Interceptors. And we cannot catch those within coroutines. It just crash the application.

2018-12-19 17:56:17.501 29846-29916/com.sample.app E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher Process: com.sample.app, PID: 29846 java.lang.Error: java.lang.Exception: Token exception at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1173) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) Caused by: java.lang.Exception: Token exception at com.sample.app.interceptors.TokenInterceptor.intercept(TokenInterceptor.kt:45) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at com.sample.app.interceptors.SentryInterceptor.intercept(SentryInterceptor.kt:17) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:200) 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) 

Enqueue enables cancellation. Please create a failing test case or executable sample for any issues.

@JakeWharton Thank you for your response.
You are right. It is imposable to cancel Deferred with call.execute() usage.
And we also have managed to handle Interceptor error. That error was not a subtype of IOException so Interceptor threw a FATAL EXCEPTION.

@VolodyaVechirko I'm having a similar issue when there is no network its throws a fatal exception. Have you any solution for this problem?