1
+ import com.apollographql.apollo.ApolloClient
2
+ import com.apollographql.apollo.api.ApolloRequest
3
+ import com.apollographql.apollo.api.ApolloResponse
4
+ import com.apollographql.apollo.api.Operation
5
+ import com.apollographql.apollo.exception.DefaultApolloException
6
+ import com.apollographql.apollo.interceptor.ApolloInterceptor
7
+ import com.apollographql.apollo.interceptor.ApolloInterceptorChain
8
+ import kotlinx.coroutines.flow.Flow
9
+ import kotlinx.coroutines.flow.flow
10
+ import kotlinx.coroutines.flow.map
11
+ import kotlinx.coroutines.runBlocking
12
+ import okio.use
13
+ import test.FooQuery
14
+ import kotlin.test.Test
15
+ import kotlin.test.assertEquals
16
+ import kotlin.test.assertFailsWith
17
+ import kotlin.test.assertIs
18
+
19
+ class InterceptorsErrorTest {
20
+ @Test
21
+ fun interceptorsMayThrow () {
22
+ ApolloClient .Builder ()
23
+ .serverUrl(" unused" )
24
+ .addInterceptor(object : ApolloInterceptor {
25
+ override fun <D : Operation .Data > intercept (
26
+ request : ApolloRequest <D >,
27
+ chain : ApolloInterceptorChain ,
28
+ ): Flow <ApolloResponse <D >> {
29
+ throw IllegalStateException (" woops" )
30
+ }
31
+ })
32
+ .build()
33
+ .use { apolloClient ->
34
+
35
+ runBlocking {
36
+ val exception = assertFailsWith<IllegalStateException > {
37
+ apolloClient.query(FooQuery ())
38
+ .execute()
39
+ }
40
+
41
+ assertEquals(" woops" , exception.message)
42
+ }
43
+ }
44
+ }
45
+
46
+ @Test
47
+ fun interceptorsMayReturnExceptionResponse () {
48
+ ApolloClient .Builder ()
49
+ .serverUrl(" unused" )
50
+ .addInterceptor(object : ApolloInterceptor {
51
+ override fun <D : Operation .Data > intercept (
52
+ request : ApolloRequest <D >,
53
+ chain : ApolloInterceptorChain ,
54
+ ): Flow <ApolloResponse <D >> {
55
+ return flow {
56
+ emit(
57
+ ApolloResponse .Builder (request.operation, request.requestUuid)
58
+ .exception(DefaultApolloException (" oh no" , IllegalStateException (" woops" )))
59
+ .build()
60
+ )
61
+ }
62
+ }
63
+ })
64
+ .build()
65
+ .use { apolloClient ->
66
+ runBlocking {
67
+ apolloClient.query(FooQuery ())
68
+ .execute()
69
+ .apply {
70
+ assertIs<DefaultApolloException >(exception)
71
+ assertIs<IllegalStateException >(exception!! .cause)
72
+ assertEquals(" woops" , exception!! .cause!! .message)
73
+ }
74
+ }
75
+ }
76
+ }
77
+ }
0 commit comments