Skip to content

Commit 0a5d12d

Browse files
authored
Catch ApolloException in toState and watchAsState (#5116)
1 parent aa154f6 commit 0a5d12d

File tree

1 file changed

+4
-0
lines changed
  • libraries/apollo-compose-support/src/main/java/com/apollographql/apollo3/compose

1 file changed

+4
-0
lines changed

libraries/apollo-compose-support/src/main/java/com/apollographql/apollo3/compose/State.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import com.apollographql.apollo3.api.Operation
1111
import com.apollographql.apollo3.api.Query
1212
import com.apollographql.apollo3.cache.normalized.watch
1313
import com.apollographql.apollo3.exception.ApolloException
14+
import com.benasher44.uuid.uuid4
15+
import kotlinx.coroutines.flow.catch
1416
import kotlin.coroutines.CoroutineContext
1517
import kotlin.coroutines.EmptyCoroutineContext
1618

@@ -35,6 +37,7 @@ import kotlin.coroutines.EmptyCoroutineContext
3537
fun <D : Operation.Data> ApolloCall<D>.toState(context: CoroutineContext = EmptyCoroutineContext): State<ApolloResponse<D>?> {
3638
val responseFlow = remember {
3739
toFlow()
40+
.catch { emit(ApolloResponse.Builder(operation, uuid4(), it as? ApolloException ?: throw it).build()) }
3841
}
3942
return responseFlow.collectAsState(initial = null, context = context)
4043
}
@@ -59,6 +62,7 @@ fun <D : Operation.Data> ApolloCall<D>.toState(context: CoroutineContext = Empty
5962
fun <D : Query.Data> ApolloCall<D>.watchAsState(context: CoroutineContext = EmptyCoroutineContext): State<ApolloResponse<D>?> {
6063
val responseFlow = remember {
6164
watch()
65+
.catch { emit(ApolloResponse.Builder(operation, uuid4(), it as? ApolloException ?: throw it).build()) }
6266
}
6367
return responseFlow.collectAsState(initial = null, context = context)
6468
}

0 commit comments

Comments
 (0)