Stylianos Gakis
08/22/2022, 1:25 PMStylianos Gakis
08/22/2022, 1:25 PMfun <D : Query.Data> ApolloCall<D>.safeWatch(): Flow<QueryResult<D>> {
return try {
watch().map(ApolloResponse<D>::toQueryResult)
} catch (apolloException: ApolloException) {
flowOf(QueryResult.Error.NetworkError(apolloException.localizedMessage))
} catch (throwable: Throwable) {
if (throwable is CancellationException) {
throw throwable
}
flowOf(QueryResult.Error.GeneralError(throwable.localizedMessage))
}
}
where .toQueryResult and QueryResult, basically a way to handle exception cases in a typed manner to avoid try catching on all the consumer sites.
But I now realized that the entire try/catch there does nothing since I’m catching errors on creating the flow, but that’s not the thing that fails, the failure happens inside the collection of flow right?
I think I want something more like:
fun <D : Query.Data> ApolloCall<D>.safeWatch(): Flow<QueryResult<D>> {
return watch(fetchThrows = true)
.map(ApolloResponse<D>::toQueryResult)
.catch { exception ->
when (exception) {
is CancellationException -> throw exception
is ApolloException -> {
emit(QueryResult.Error.NetworkError(exception.localizedMessage))
}
else -> {
emit(QueryResult.Error.GeneralError(exception.localizedMessage))
}
}
}
}
right?Stylianos Gakis
08/22/2022, 1:30 PMexecute()
. Have you tried abstracting this a bit before with smth like what I was trying to do here? Maybe there’s an opportunity for a lib on top of apollo, kinda like what EitherNet from slack does for Retrofit. But maybe this isn’t optimal for Apollo since you also need to model the partial success cases 🤔bod
08/22/2022, 1:39 PM.catch
and try / catch
is that catch
will not be called for a cancelation - which is usually what you want 🙂bod
08/22/2022, 1:46 PMis CancellationException
in the snippet above is probably unneeded)Stylianos Gakis
08/22/2022, 1:50 PM