kotlinapollo-clientapollo-androidapollo-kotlin

How to catch exceptions from ApolloClient watch method?


This code - manual query execution without watch - throws an exception as I expect it (due to an intentionally wrong server url not shown here):

try {
    val response = apolloClient.query(MyQuery()).execute()
} catch (e: Exception) {
    Timber.e("exception from manual execution")
    Timber.e(e)
}

However, if instead I use watch (and intentionally the same wrong server url) like in the following code, I get no exception:

try {
    apolloClient.query(MyQuery())
        .watch() // flow
        .collect { response ->
            Timber.i("apollo watch collect called")
        }
} catch (e: Exception) {
    Timber.e("exception from watch execution")
    Timber.e(e)
}

Where is my error? Why don't I get an exception in the second case? How to catch errors when using watch?


Solution

  • apolloClient.query(MyQuery())
            .watch( fetchThrows = true, refetchThrows = true) // need this
            .catch{t->}//catch here
            .collect { response ->
                Timber.i("apollo watch collect called")
            }