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

Catch Error if JSON.Stringify Fails for Engine Trace #1668

Merged
merged 3 commits into from
Sep 14, 2018
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
12 changes: 6 additions & 6 deletions packages/apollo-engine-reporting/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ export class EngineReportingAgent<TContext = any> {
message.byteOffset,
message.byteLength,
);
gzip(messageBuffer, (err, compressed) => {
gzip(messageBuffer, (err, gzipResult) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These changes were not related to this PR. They were tslint errors (no-shadowed-variable)

if (err) {
reject(err);
} else {
resolve(compressed);
resolve(gzipResult);
}
});
});
Expand All @@ -232,7 +232,7 @@ export class EngineReportingAgent<TContext = any> {
// Retry on network errors and 5xx HTTP
// responses.
async () => {
const response = await fetch(endpointUrl, {
const curResponse = await fetch(endpointUrl, {
method: 'POST',
headers: {
'user-agent': 'apollo-engine-reporting',
Expand All @@ -242,10 +242,10 @@ export class EngineReportingAgent<TContext = any> {
body: compressed,
});

if (response.status >= 500 && response.status < 600) {
throw new Error(`${response.status}: ${response.statusText}`);
if (curResponse.status >= 500 && curResponse.status < 600) {
throw new Error(`${curResponse.status}: ${curResponse.statusText}`);
} else {
return response;
return curResponse;
}
},
{
Expand Down
15 changes: 12 additions & 3 deletions packages/apollo-engine-reporting/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,18 @@ export class EngineReportingExtension<TContext = any>
// will be sent as '""'.
this.trace.details!.variablesJson![name] = '';
} else {
this.trace.details!.variablesJson![name] = JSON.stringify(
o.variables![name],
);
try {
this.trace.details!.variablesJson![name] = JSON.stringify(
o.variables![name],
);
} catch (e) {
// This probably means that the value contains a circular reference,
// causing `JSON.stringify()` to throw a TypeError:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Issue_with_JSON.stringify()_when_serializing_circular_references
this.trace.details!.variablesJson![name] = JSON.stringify(
'[Unable to convert value to JSON]',
);
}
}
});
}
Expand Down