fetchPolicy `cache-and-network` throwing an unhandled promise rejection
See original GitHub issueOriginally 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:
- Created 6 years ago
- Comments:8 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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#L363So 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
andyarn start
.The executed query uses
cache-and-network
policy, usingwatchQuery
, and thenext
callback prints to log, anderror
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.
(Note the
UnhandledPromiseRejectionWarning
)When removing the
cachePolicy
line, the custom error handler can catch it, and the custom error handler prints: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.BTW, @jbaxleyiii, I guess that we can also fix https://github.com/apollographql/apollo-client/issues/2271 with a similar solution