https://kotlinlang.org logo
#coroutines
Title
# coroutines
g

georgiy.shur

07/02/2019, 5:35 AM
Hello, what is the proper way to handle error/success responses with new Retrofit coroutines? Back then we did it with custom adapter. But now it seems that it's not possible with corourines.
g

gildor

07/02/2019, 5:57 AM
Custom adapter?
in general standard way is to use try/catch on call site, if you want to have some global error handler it should work with okhttp Interceptor
g

georgiy.shur

07/02/2019, 6:35 AM
Sorry, yes I meant global handling for common errors. With Calls and rx it was possible to set the custom adapter while creating Retrofit instance which transforms the errors into our format or even handles it before we get a response from api service. But I think with new corourines it isn't possible.
j

Javier

07/02/2019, 7:38 AM
I am trying to get a custom adapter too but no success yet.
b

bdawg.io

07/02/2019, 2:39 PM
There is no call adapter with suspend support. The continuation becomes the adapter.
I tried a bit but I didn't get a solution yet, if I got it I will add here and in reddit too.
c

Chris Craik

07/02/2019, 4:03 PM
You can wrap each call with another function, and re-throw the error so you can see the error globally with your wrapper, as well as at each callsite.
j

jw

07/02/2019, 4:08 PM
@bdawg.io this is not true. suspend is built on the
CallAdapter
for
Call
and you are free to intercept it and mix in any behavior you want
b

bdawg.io

07/02/2019, 4:09 PM
@jw yeah that reddit reply made sense on how it was implemented 🙂. I didn't consider adapting for
Call
by evaluating the type parameter
g

georgiy.shur

07/03/2019, 2:36 PM
Thank you guys, for now I came up with the solution using
Interceptor
, but I'll give a try to adapter as well. What do you think by design is a better place to handle common errors, interceptor or adapter?
j

Javier

07/05/2019, 4:45 PM
@georgiy.shur I think it is better in the adapter.
👍 1
@jw I was trying to create this adapter but I have a problem. With the RX Adapter there is no problem because you can instantiate the Observable and when the onqueue/onfailure methods is finishing, they emit a new value. But with a sealed class I can't instantiate it before onqueue/onfailure methods start and later emit a new value because it is not an Observable.
9 Views