Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rethrow caught errors in setTimeout #1367

Merged
merged 3 commits into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,12 @@ export class QueryManager {
try {
observer.error(apolloError);
} catch (e) {
console.error('Error in observer.error \n', e.stack);
// Throw error outside this control flow to avoid breaking Apollo's state
setTimeout(() => { throw e; }, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad you put a comment 👍

}
} else {
console.error('Unhandled error', apolloError, apolloError.stack);
// Throw error outside this control flow to avoid breaking Apollo's state
setTimeout(() => { throw apolloError; }, 0);
if (!isProduction()) {
/* tslint:disable-next-line */
console.info(
Expand Down Expand Up @@ -430,7 +432,8 @@ export class QueryManager {
try {
observer.next(maybeDeepFreeze(resultFromStore));
} catch (e) {
console.error('Error in observer.next \n', e.stack);
// Throw error outside this control flow to avoid breaking Apollo's state
setTimeout(() => { throw e; }, 0);
}
}
}
Expand Down
60 changes: 33 additions & 27 deletions test/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,21 @@ describe('client', () => {
});
});

it('should not let errors in observer.next reach the store', (done) => {

it('should surface errors in observer.next as uncaught', (done) => {
const expectedError = new Error('this error should not reach the store');
const listeners = process.listeners('uncaughtException');
const oldHandler = listeners[listeners.length - 1];
const handleUncaught = (e: Error) => {
process.removeListener('uncaughtException', handleUncaught);
process.addListener('uncaughtException', oldHandler);
if (e === expectedError) {
done();
} else {
done(e);
}
};
process.removeListener('uncaughtException', oldHandler);
process.addListener('uncaughtException', handleUncaught);
const query = gql`
query people {
allPeople(first: 1) {
Expand All @@ -494,7 +507,7 @@ it('should not let errors in observer.next reach the store', (done) => {
}
`;

const data = {
const data = {
allPeople: {
people: [
{
Expand All @@ -516,24 +529,28 @@ it('should not let errors in observer.next reach the store', (done) => {

const handle = client.watchQuery({ query });

const consoleDotError = console.error;
console.error = (err: string) => {
console.error = consoleDotError;
if (err.match(/Error in observer.next/)) {
done();
} else {
done(new Error('Expected error in observer.next to be caught'));
}
};

handle.subscribe({
next(result) {
throw new Error('this error should not reach the store');
throw expectedError;
},
});
});

it('should not let errors in observer.error reach the store', (done) => {
it('should surfaces errors in observer.error as uncaught', (done) => {
const expectedError = new Error('this error should not reach the store');
const listeners = process.listeners('uncaughtException');
const oldHandler = listeners[listeners.length - 1];
const handleUncaught = (e: Error) => {
process.removeListener('uncaughtException', handleUncaught);
process.addListener('uncaughtException', oldHandler);
if (e === expectedError) {
done();
} else {
done(e);
}
};
process.removeListener('uncaughtException', oldHandler);
process.addListener('uncaughtException', handleUncaught);

const query = gql`
query people {
Expand All @@ -556,23 +573,12 @@ it('should not let errors in observer.next reach the store', (done) => {
});

const handle = client.watchQuery({ query });

const consoleDotError = console.error;
console.error = (err: string) => {
console.error = consoleDotError;
if (err.match(/Error in observer.error/)) {
done();
} else {
done(new Error('Expected error in observer.error to be caught'));
}
};

handle.subscribe({
next() {
done(new Error('did not expect next to be called'));
},
error(err) {
throw new Error('this error should not reach the store');
throw expectedError;
},
});
});
Expand Down