https://kotlinlang.org logo
#apollo-kotlin
Title
# apollo-kotlin
e

Eduard Boloș

09/20/2022, 3:52 PM
Hello! It's very possible that I am doing something wrong, or I am just confused, but I have some trouble with error handling, maybe someone can help? 🙏 Basically, when I do
apolloCall.execute()
, in case of a network error (e.g. no internet, backend down), instead of a
ApolloNetworkException
to be raised, I get
java.util.NoSuchElementException: Flow is empty
. Which I can see being thrown by the
.first()
in `execute`'s implementation. This happens although I can see clearly that a
ApolloNetworkException
is thrown in
DefaultHttpEngine.execute()
. Am I doing something wrong? Or what might be happening?
😞 [Narrator] He was doing something wrong
1
m

mbonnin

09/20/2022, 3:58 PM
Interesting...
Do you use the cache and a specific fetchPolicy maybe?
e

Eduard Boloș

09/20/2022, 4:00 PM
Yes, normalized cache is set up, but this query is using the NetworkOnly fetch policy
m

mbonnin

09/20/2022, 4:00 PM
That might be the "issue"
The error handling has become a bit complex there (see also https://github.com/apollographql/apollo-kotlin/issues/4003)
Looks like the
ApolloNetworkException
is caught somewhere and makes the
.first()
fail 🤔
You're not using
.watch()
or anything like that, right?
(
.watch()
ignores errors by default)
e

Eduard Boloș

09/20/2022, 4:04 PM
not in this flow, no
m

mbonnin

09/20/2022, 4:04 PM
Let me try to reproduce quickly
e

Eduard Boloș

09/20/2022, 4:05 PM
Looks like the
ApolloNetworkException
is caught somewhere
Yeah, that seems to be the case, actually. Even if I do
toFlow().catch { Log.e(TAG, "error", it) }
, nothing ever gets logged.
m

mbonnin

09/20/2022, 4:16 PM
Integration tests trigger an
ApolloNetworkException
as expected https://github.com/apollographql/apollo-kotlin/commit/95ce6b84e71958fe8b4b9c2d1c7cc3a5612778ec
The code is pretty simple:
Copy code
fun networkOnly() = runTest(before = { setUp() }, after = { tearDown() }) {
    val query = HeroNameQuery()
    val data = HeroNameQuery.Data(HeroNameQuery.Hero("R2-D2"))

    val call = apolloClient.query(query).fetchPolicy(FetchPolicy.NetworkOnly)

    // First query should hit the network and save in cache
    mockServer.enqueue(query, data)
    val response = call.execute()
e

Eduard Boloș

09/20/2022, 4:16 PM
hmm, let me check something
ok, it was me, of course 🤦‍♂️ I added an interceptor for logging, but I didn't rethrow the exception in the
catch
block
sorry for wasting your time 😞
m

mbonnin

09/20/2022, 4:20 PM
No pb, glad we found the cause!
e

Eduard Boloș

09/20/2022, 4:21 PM
yeah, thank you for the quick response and pointing out that it's not the library 😄
m

mbonnin

09/20/2022, 4:21 PM
Sure thing!
16 Views