diff --git a/CHANGELOG.md b/CHANGELOG.md index 85dbafd6de8..e375d08e7a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Expect active development and potentially significant breaking changes in the `0 ### vNEXT +- Use `console.error` to log unhandled network errors. [Issue #189](https://github.com/apollostack/apollo-client/issues/189) [PR #203](https://github.com/apollostack/apollo-client/pull/203) - ... ### v0.3.5 diff --git a/src/QueryManager.ts b/src/QueryManager.ts index ecb4d4c7d13..8a86cd3d00f 100644 --- a/src/QueryManager.ts +++ b/src/QueryManager.ts @@ -188,6 +188,10 @@ export class QueryManager { // XXX we might not want to re-broadcast the same error over and over if it didn't change if (observer.error) { observer.error(queryStoreValue.networkError); + } else { + console.error('Unhandled network error', + queryStoreValue.networkError, + queryStoreValue.networkError.stack); } } else { const resultFromStore = readSelectionSetFromStore({ diff --git a/test/QueryManager.ts b/test/QueryManager.ts index 2c80a4d3d92..8f922a1e908 100644 --- a/test/QueryManager.ts +++ b/test/QueryManager.ts @@ -354,6 +354,53 @@ describe('QueryManager', () => { }); }); + it('uses console.error to log unhandled errors', (done) => { + const query = gql` + query people { + allPeople(first: 1) { + people { + name + } + } + } + `; + + const networkInterface = mockNetworkInterface( + { + request: { query }, + error: new Error('Network error'), + } + ); + + const queryManager = new QueryManager({ + networkInterface, + store: createApolloStore(), + reduxRootKey: 'apollo', + }); + + const handle = queryManager.watchQuery({ + query, + }); + + const oldError = console.error; + let printed; + console.error = (...args) => { + printed = args; + }; + + handle.subscribe({ + next: (result) => { + done(new Error('Should not deliver result')); + }, + }); + + setTimeout(() => { + assert.match(printed[0], /error/); + console.error = oldError; + done(); + }, 10); + }); + it('handles an unsubscribe action that happens before data returns', (done) => { const query = gql` query people { @@ -1463,7 +1510,6 @@ describe('QueryManager', () => { request: { query, variables }, result: { data: data2 }, } - ); const queryManager = new QueryManager({ @@ -1487,13 +1533,13 @@ describe('QueryManager', () => { assert.deepEqual(result.data, data1); } else if (handleCount === 2) { assert.deepEqual(result.data, data2); + subscription.unsubscribe(); done(); } }, }); subscription.startPolling(50); - }); it('exposes a way to stop a polling query', (done) => { const query = gql`