question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not able to Catch Error thrown from client.query

See original GitHub issue

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 –>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
rocketramancommented, Jan 31, 2020

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.

8reactions
t3dotggcommented, Apr 11, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't I catch error thrown from node-postgres?
I've tried numerous different approaches - The Async/Await Try/Catch method, shown above - I've tried pgPool.query().then().catch() - ...
Read more >
Handling operation errors - Apollo GraphQL Docs
Apollo Client helps you handle these errors according to their type, enabling you to show appropriate information to the user when an error...
Read more >
React Query Error Handling | TkDodo's blog
One thing that Error Boundaries cannot do is catch asynchronous errors, because those do not occur during rendering. So to make Error Boundaries ......
Read more >
Automatically handling Apollo Client errors in your React UI
Next, once your server has been able to parse a query ... concerned that a global error handler will make it difficult to...
Read more >
Handling Errors with Apollo Client (React) - YouTube
In this video I talk about handling errors when running queries and mutations using Apollo Client in a React project.Table of contents:00:00 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found