Are there examples on how to handle exceptions dur...
# coroutines
a
Are there examples on how to handle exceptions during
launch
blocks? I have a method created like so:
Copy code
suspend fun <T> Deferred<T>.awaitOrRedirect(context: Context): T? =
    try {
        await()
    } catch (e: Exception) {
        logd("Error, redirect to Login")
        LoginActivity.launch(context)
        null
    }
But the errors seem to just pass through and cancel the coroutines altogether without launching the activity. To give more details, I’m building for Android, and I’m using
CoroutineScrope
within the activity that is calling the method. I’m using
launch
as it is used for most of the example code, and it looks like the only difference with
async
is that
async
cancels the parent and returns a deferred value. I also see that there are `CoroutineExceptionHandler`s, though I’m not sure if that is the right fit here.
g
But in your example there is no launch, only Deferred and await Your code looks fine, should work
Remember that launch and async has different way to handle exceptions
a
To give more details, this is for the coroutine implementation for retrofit. I am calling this method in a launch within an Activity. The expectation that an exception will trigger a login attempt as the api call has failed. For some reason the launching doesn't always work, and the app stops as a whole.
I will try again later on. I spent some time looking at kotlin conf 17, though I still don't know what is wrong.
g
coroutine implementation for retrofit
which one?
the app stops as a whole
do you mean sometimes app crashes? Are you sure that this method is the reason?
Hmmm, If I understood your use case correctly, you want to write a function that opens login activity if request failed and replace
await()
? If so, I would say that it’s wrong approach. Much better to use custom okhttp Interceptor that does this for you
a
This adapter: https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter This is also my first attempt at jetpack, so the problem could potentially be elsewhere. I agree that okhttp interceptor is a better way, but it doesn't have access to the context necessary to launch the login activity. Doing so with coroutines should also be possible, so I want to use this as an opportunity to better understand how it works
g
but it doesn’t have access to the context necessary to launch the login activity
Just pass application context to Interceptor’s constructor, no problems there, just start activity in a new task (you cannot start it in the same task without activity context) Another approach: interceptor just send some event that can be caught by current active activity and show login screen on the same task
a
I currently can no longer replicate the issue so it may be a problem elsewhere. Using the interceptor approach is an interesting idea, and I think that passing the exception to the coroutine will also help with stopping other api requests as it redirects to login. Thanks for your help!