Not able to Catch Error thrown from client.query
See original GitHub issueIntended 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 –>
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (1 by maintainers)
Top GitHub Comments
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.
errorPolicy: 'all'
defaults to returning the data and error.errorPolicy: 'none'
will catch all errors and prevent data from being returned.