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.

fetchPolicy `cache-and-network` throwing an unhandled promise rejection

See original GitHub issue

Originally started here: https://github.com/apollographql/react-apollo/issues/1117

When using cache-and-network and the server throws an error, the promise does not return to the executor, and it’s not possible to catch and handle the error.

The actual code is here: https://github.com/apollographql/apollo-client/blob/a1217fbd1ebe8b56ff96dcb077430720dd0b381c/src/core/QueryManager.ts#L505-L507

Intended outcome: Promise should return, so the callee can use .catch(...) on the promise.

Actual outcome: Return value it undefined, and the callee can’t handle the error.

How to reproduce the issue: Use catch-and-network fetch policy, and cause the server to fail the query.

Version

  • apollo-client@1.9.3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
dotansimhacommented, Oct 10, 2017

@jbaxleyiii It looks like the code is the same under master branch: https://github.com/apollographql/apollo-client/blob/master/packages/apollo-client/src/core/QueryManager.ts#L363

So if the promise does not return from the function, the callee can’t handle a rejection.

To see a running example with the error, clone this: https://github.com/dotansimha/apollo-client-unhandled-expection , under the cache-and-network directory.

Run yarn and yarn start.

The executed query uses cache-and-network policy, using watchQuery, and the next callback prints to log, and error callback prints the error with a custom text.

You will notice that the error is thrown from the resolver (sever-side), and the client unable to handle it. The error is first caught with the custom error handler, and then the original callee of the query can’t catch it, and it causing an unhandled promise rejection.

{ data: undefined, loading: true, networkStatus: 1, stale: true }
error caught: { Error: GraphQL error: oops
    at new ApolloError (/Users/dotansimha/Dev/temp3/node_modules/apollo-client/apollo.umd.js:1981:28)
    at /Users/dotansimha/Dev/temp3/node_modules/apollo-client/apollo.umd.js:3091:41
    at /Users/dotansimha/Dev/temp3/node_modules/apollo-client/apollo.umd.js:3583:25
    at Array.forEach (<anonymous>)
    at /Users/dotansimha/Dev/temp3/node_modules/apollo-client/apollo.umd.js:3580:27
    at Array.forEach (<anonymous>)
    at QueryManager.broadcastQueries (/Users/dotansimha/Dev/temp3/node_modules/apollo-client/apollo.umd.js:3577:42)
    at /Users/dotansimha/Dev/temp3/node_modules/apollo-client/apollo.umd.js:3512:31
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
  graphQLErrors:
   [ { Error: oops
    at posts (/Users/dotansimha/Dev/temp3/index.js:47:13)
    at resolveFieldValueOrError (/Users/dotansimha/Dev/temp3/node_modules/graphql/execution/execute.js:498:12)
    at resolveField (/Users/dotansimha/Dev/temp3/node_modules/graphql/execution/execute.js:462:16)
    at /Users/dotansimha/Dev/temp3/node_modules/graphql/execution/execute.js:311:18
    at Array.reduce (<anonymous>)
    at executeFields (/Users/dotansimha/Dev/temp3/node_modules/graphql/execution/execute.js:308:42)
    at executeOperation (/Users/dotansimha/Dev/temp3/node_modules/graphql/execution/execute.js:236:122)
    at executeImpl (/Users/dotansimha/Dev/temp3/node_modules/graphql/execution/execute.js:133:26)
    at execute (/Users/dotansimha/Dev/temp3/node_modules/graphql/execution/execute.js:110:229)
    at InBrowserNetworkInterface.query (/Users/dotansimha/Dev/temp3/index.js:88:12) message: 'oops', locations: undefined, path: [Array] } ],
  networkError: null,
  message: 'GraphQL error: oops',
  extraInfo: undefined }

(node:2926) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: GraphQL error: oops
(node:2926) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

(Note the UnhandledPromiseRejectionWarning)

When removing the cachePolicy line, the custom error handler can catch it, and the custom error handler prints:

error caught: { Error: GraphQL error: oops
    at new ApolloError (/Users/dotansimha/Dev/temp3/node_modules/apollo-client/apollo.umd.js:1981:28)
    at /Users/dotansimha/Dev/temp3/node_modules/apollo-client/apollo.umd.js:3091:41
    at /Users/dotansimha/Dev/temp3/node_modules/apollo-client/apollo.umd.js:3583:25
    at Array.forEach (<anonymous>)
    at /Users/dotansimha/Dev/temp3/node_modules/apollo-client/apollo.umd.js:3580:27
    at Array.forEach (<anonymous>)
    at QueryManager.broadcastQueries (/Users/dotansimha/Dev/temp3/node_modules/apollo-client/apollo.umd.js:3577:42)
    at /Users/dotansimha/Dev/temp3/node_modules/apollo-client/apollo.umd.js:3512:31
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

This issue is very similar to another issue I opened: https://github.com/apollographql/apollo-client/issues/2271 It’s a real pain for me because I am using react-native and unhandled promise rejection causing the app to crash.

1reaction
dotansimhacommented, Oct 11, 2017

BTW, @jbaxleyiii, I guess that we can also fix https://github.com/apollographql/apollo-client/issues/2271 with a similar solution

Read more comments on GitHub >

github_iconTop Results From Across the Web

Queries - Apollo GraphQL Docs
A cache-only query throws an error if the cache does not contain data for all requested fields. cache-and-network. Apollo Client executes the full...
Read more >
caching promises leads to Unhandled promise rejection in ...
This issue is happening because it is not the original promise that ultimately fails, it is the then() ! You are not storing...
Read more >
api documentation for apollo-client (v1.0.4)
fetchPolicy === 'cache-and-network') { throw new Error('cache-and-network ... options = _a.options; return new Promise(function (resolve, reject) { var ...
Read more >
@apollo/client | Yarn - Package Manager
... execute functions from causing unhandled Promise rejection errors if uncaught. ... Fix SSR and cache-and-network fetch policy (@dastoori in #3372) ...
Read more >
apollo-client/bundle.esm.js.map - UNPKG
TVariables): Promise<ApolloQueryResult<TData>> {\n let { fetchPolicy } = this.options ... onError(err);\n return;\n }\n invariant.error('Unhandled GraphQL ...
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