It's an Android app, I'm doing some async network ...
# coroutines
k
It's an Android app, I'm doing some async network requests and then I'm manipulation the UI. The network requests can throw `IOException`s or `HttpException`s so I need to catch them, otherwise the app will crash.
e
Some you can catch just `IOException`s, can't you?
k
and
HttpException
, at least (multi catch would be useful)
but I'm also catching other exceptions and submit them to my crash reporting instead of letting the app crash
e
And how do you handle your IOExceptions? Like some specific code everywhere, so you’ve encapsulated the way you handle IOExceptions into some common code?
k
I have some generic retry logic for the httprequests (I'm using Rx for this) but if it fails after n times, I just swallow it and set the UI to some fail state
@elizarov so
if (e is CancellationException) throw e
is ok in my catch block?
e
I see thanks. Yes, it is Ok. You can also have the same line in your crash reporter.
k
ok, thanks for help. do you think there is a way, to streamline this?
e
I would extract into some some common code. Actually, I’d install a crash reporter as
CoroutineExceptionHandler
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-exception-handler/index.html in my context and make it ignore
CancellationException
, but report everything else
k
but where do I put the UI code then?
e
Basically, the idea is that instead of doing
launch(UI) { … }
to start a coroutine you do
luanch(myOwnContext) { … }
, where
myOwnContext = UI + myExceptionHandler
👍 1
c
@kirillrakhman How do you deal with this?
k
@cbruegg currently I don't
i.e. I have automatic cancelation based on Navi, but I don't check the lifecycle after every suspension point
@elizarov I understand, but I still need to know when the coroutine fails, so I need to extend my exception logger context and provide that?
e
It is hard to get a more specific advice without looking at your actual code
c
Are you sure though your jobs are actually canceled even when they're already in the Handler queue? (If you don't know what I mean and have the time, feel free check out the example project I've linked to)