Hello, what is the proper way to handle error/succ...
# coroutines
g
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
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
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
I am trying to get a custom adapter too but no success yet.
b
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
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
@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
@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
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
@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.