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

GraphQLError: keep extensions always present #3313

Merged
merged 1 commit into from Oct 13, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/error/GraphQLError.ts
Expand Up @@ -61,7 +61,7 @@ export class GraphQLError extends Error {
/**
* Extension fields to add to the formatted error.
*/
readonly extensions: { [key: string]: unknown } | undefined;
readonly extensions: { [key: string]: unknown };

constructor(
message: string,
Expand Down Expand Up @@ -124,7 +124,7 @@ export class GraphQLError extends Error {
? originalError?.extensions
: undefined;
// TODO: merge `extensions` and `originalExtensions`
this.extensions = extensions ?? originalExtensions;
this.extensions = extensions ?? originalExtensions ?? Object.create(null);

// Include (non-enumerable) stack trace.
if (originalError?.stack) {
Expand Down Expand Up @@ -183,7 +183,7 @@ export class GraphQLError extends Error {
formattedError.path = this.path;
}

if (this.extensions != null) {
if (this.extensions != null && Object.keys(this.extensions).length > 0) {
formattedError.extensions = this.extensions;
}

Expand Down
8 changes: 6 additions & 2 deletions src/error/__tests__/GraphQLError-test.ts
Expand Up @@ -28,10 +28,14 @@ describe('GraphQLError', () => {
expect(new GraphQLError('str')).to.be.instanceof(GraphQLError);
});

it('has a name, message, and stack trace', () => {
it('has a name, message, extensions, and stack trace', () => {
const e = new GraphQLError('msg');

expect(e).to.include({ name: 'GraphQLError', message: 'msg' });
expect(e).to.deep.include({
name: 'GraphQLError',
message: 'msg',
extensions: {},
});
expect(e.stack).to.be.a('string');
});

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Expand Up @@ -8,6 +8,7 @@
"useUnknownInCatchVariables": false,
"noEmit": true,
"isolatedModules": true,
"importsNotUsedAsValues": "error",
"forceConsistentCasingInFileNames": true
}
}