Skip to content

Commit

Permalink
catching circular reference errors, adding test
Browse files Browse the repository at this point in the history
  • Loading branch information
Helen Ho committed Jun 27, 2019
1 parent ef3b7be commit 90ef77c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 13 additions & 0 deletions packages/apollo-engine-reporting/src/__tests__/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,19 @@ describe('variableJson output for sendVariableValues transform: custom function
});
});

describe('Catch circular reference error during JSON.stringify', () => {
const circularReference = {};
circularReference['this'] = circularReference;

const circularVariables = {
bad: circularReference,
};

expect(
makeTraceDetails(circularVariables, { all: true }).variablesJson['bad'],
).toEqual(JSON.stringify('[Unable to convert value to JSON]'));
});

function makeTestHTTP(): Trace.HTTP {
return new Trace.HTTP({
method: Trace.HTTP.Method.UNKNOWN,
Expand Down
10 changes: 8 additions & 2 deletions packages/apollo-engine-reporting/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,15 @@ export function makeTraceDetails(
} catch (e) {
if (
e.name === 'TypeError' &&
e.message === 'Converting circular structure to JSON'
(e.message === 'cyclic object value' || // firefox
e.message === 'Converting circular structure to JSON' || // chrome, opera
e.message === 'Circular reference in value argument not supported') // edge
// May be unnecessary to check against each browser
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#Message
) {
details.variablesJson![name] = JSON.stringify(e.message);
details.variablesJson![name] = JSON.stringify(
'[Unable to convert value to JSON]',
);
} else {
// Re-throw when it doesn't meet our expectation so we don't
// inadvertently swallow anything as a "cycle error" when its not.
Expand Down

0 comments on commit 90ef77c

Please sign in to comment.