Skip to content

Commit

Permalink
GraphQLError: keep extensions always present (#3313)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Oct 13, 2021
1 parent 82900fa commit b1ce2c3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
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
}
}

0 comments on commit b1ce2c3

Please sign in to comment.