Hello! It's very possible that I am doing somethin...
# apollo-kotlin
e
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
Interesting...
Do you use the cache and a specific fetchPolicy maybe?
e
Yes, normalized cache is set up, but this query is using the NetworkOnly fetch policy
m
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
not in this flow, no
m
Let me try to reproduce quickly
e
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
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
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
No pb, glad we found the cause!
e
yeah, thank you for the quick response and pointing out that it's not the library 😄
m
Sure thing!