Apollo-client: Not able to Catch Error thrown from client.query

Created on 7 Mar 2019  路  4Comments  路  Source: apollographql/apollo-client

Intended outcome:
i wanted to see the GraphQLError in the Promise console.log('ERRORS ARE => ', res.errors); get printed post the Apolloclient.query call

Actual outcome:
I can see in my chrome console that i get 2 errors that is
1>GraphQLError & followed by NetworkError getting printed ( from a file node_modules/ts-invariant/lib/invariant.esm.js ) and in my app although i have below code, it always goes to catch block and prints NetWorkError

return await client.query({ query: findAgreementQuery, variables: { InteractionLineId, ClientChannelId, MobileDeviceNumber, CountryCallingCode }, fetchPolicy: 'no-cache', errorPolicy: 'all' }) .then(res => { console.log('DATA IS =>', res.data); console.log('ERRORS ARE => ', res.errors); return res.data; }) .catch(error => { console.log('in CATCH BLOCK ', error); }); }
-->

How to reproduce the issue:
create a ApolloClient instance and call a query on that instance and try to catch the error when you have teh query defined wrong and see what error you get

Versions
apollo client 2.5.1
-->

Most helpful comment

errorPolicy: 'all' defaults to returning the data and error. errorPolicy: 'none' will catch all errors and prevent data from being returned.

All 4 comments

errorPolicy: 'all' defaults to returning the data and error. errorPolicy: 'none' will catch all errors and prevent data from being returned.

I'd close this one, TheoBr explained that this is not an issue but a configuration option.

The OP doesn't specify whether his network response was a 2xx response or not, but given the existence of a NetworkError in his logs, I would assume not. I just encountered this problem as well, with client apollo-client 2.6.8, and errorPolicy: 'all' does not fix it -- the query or mutation error handler still only gets the NetworkError, and not the GraphQLError's contained in the response.

It appears that in this situation where the client receives a non-2xx response from the server, and the response body contains GraphQLErrors, the client is "forgetting" to send the GraphQLErrors to the query/mutation error handler.

See here for another user that encountered the same issue: https://spectrum.chat/apollo/apollo-client/why-is-apollo-client-not-handling-my-mutation-errors-properly~dc806c3a-4325-4288-9f9a-6aeaa278b484.

I have this same problem with a Mutation. This is a bug and changing errorPolicy does not fix it.

This happens when there's no data in the response... it only works as expected when both errors and data are present, e.g.

errors: {聽鈥β爙,
data: { myMutationName: null },

But, let imagine you have a mutation, and during runtime you pass the wrong variables... the response from the server is correct (it has all the error details) BUT, Apollo Client throws, because it's trying to resolve the Mutation name on something that is undefined (data). The result is that I can't handle errors on client side. And this is the stack trace:

TypeError: Cannot read property 'myMutationName' of undefined
    at LocalState.<anonymous> (bundle.esm.js:996)
    at step (tslib.es6.js:208)
    at Object.next (tslib.es6.js:138)
    at tslib.es6.js:111
    at new Promise (<anonymous>)
    at __awaiter (tslib.es6.js:88)
    at LocalState.resolveField (bundle.esm.js:986)
    at LocalState.<anonymous> (bundle.esm.js:947)
    at step (tslib.es6.js:208)
    at Object.next (tslib.es6.js:138)
    at tslib.es6.js:111
    at new Promise (<anonymous>)
    at __awaiter (tslib.es6.js:88)
    at execute (bundle.esm.js:939)
    at Array.map (<anonymous>)
    at LocalState.<anonymous> (bundle.esm.js:978)
    at step (tslib.es6.js:208)
    at Object.next (tslib.es6.js:138)
    at tslib.es6.js:111
    at new Promise (<anonymous>)
    at __awaiter (tslib.es6.js:88)
    at LocalState.resolveSelectionSet (bundle.esm.js:929)
    at LocalState.<anonymous> (bundle.esm.js:918)
    at step (tslib.es6.js:208)
    at Object.next (tslib.es6.js:138)
    at tslib.es6.js:111
    at new Promise (<anonymous>)
    at __awaiter (tslib.es6.js:88)
    at LocalState.resolveDocument (bundle.esm.js:896)
    at LocalState.<anonymous> (bundle.esm.js:769)
    at step (tslib.es6.js:208)
    at Object.next (tslib.es6.js:138)
    at tslib.es6.js:111
    at new Promise (<anonymous>)
    at __awaiter (tslib.es6.js:88)
    at LocalState.runResolvers (bundle.esm.js:766)
    at bundle.esm.js:2058
    at bundle.esm.js:1114
    at new Promise (<anonymous>)
    at Object.next (bundle.esm.js:1113)
    at notifySubscription (Observable.js:152)
    at onNotify (Observable.js:196)
    at SubscriptionObserver.next (Observable.js:248)
    at bundle.esm.js:1080
    at Set.forEach (<anonymous>)
    at Object.next (bundle.esm.js:1079)
    at notifySubscription (Observable.js:152)
    at onNotify (Observable.js:196)
    at SubscriptionObserver.next (Observable.js:248)
    at Object.next (Observable.js:338)

Please let us know any possible fix with v2, or if this is solved on v3.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings