Skip to content

Commit

Permalink
GraphQLError: Fixed originalError.extensions overriding `extensions…
Browse files Browse the repository at this point in the history
…` argument to constructor (#3343)

Co-authored-by: Ivan Goncharov <[email protected]>
  • Loading branch information
klippx and IvanGoncharov authored Oct 28, 2021
1 parent 7dc29fd commit 188122b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/error/GraphQLError.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ export class GraphQLError extends Error {

this.path = path ?? undefined;

this.extensions = extensions ?? {};

const originalExtensions = originalError?.extensions;
if (isObjectLike(originalExtensions)) {

if (extensions == null && isObjectLike(originalExtensions)) {
this.extensions = { ...originalExtensions };
} else {
this.extensions = extensions ?? {};
}

// By being enumerable, JSON.stringify will include bellow properties in the resulting output.
Expand Down
60 changes: 60 additions & 0 deletions src/error/__tests__/GraphQLError-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,66 @@ describe('GraphQLError', () => {
});
});

it('defaults to original error extension only if extensions argument is not passed', () => {
class ErrorWithExtensions extends Error {
extensions: mixed;

constructor(message: string) {
super(message);
this.extensions = { original: 'extensions' };
}
}

const original = new ErrorWithExtensions('original');
const inheritedExtensions = new GraphQLError(
'InheritedExtensions',
undefined,
undefined,
undefined,
undefined,
original,
undefined,
);

expect(inheritedExtensions).to.deep.include({
message: 'InheritedExtensions',
originalError: original,
extensions: { original: 'extensions' },
});

const ownExtensions = new GraphQLError(
'OwnExtensions',
undefined,
undefined,
undefined,
undefined,
original,
{ own: 'extensions' },
);

expect(ownExtensions).to.deep.include({
message: 'OwnExtensions',
originalError: original,
extensions: { own: 'extensions' },
});

const ownEmptyExtensions = new GraphQLError(
'OwnEmptyExtensions',
undefined,
undefined,
undefined,
undefined,
original,
{},
);

expect(ownEmptyExtensions).to.deep.include({
message: 'OwnEmptyExtensions',
originalError: original,
extensions: {},
});
});

it('serializes to include all standard fields', () => {
const eShort = new GraphQLError('msg');
expect(JSON.stringify(eShort, null, 2) + '\n').to.equal(dedent`
Expand Down

0 comments on commit 188122b

Please sign in to comment.