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: Fixed originalError.extensions overriding extensions argument to constructor #3343

Merged
merged 2 commits into from Oct 28, 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
7 changes: 4 additions & 3 deletions src/error/GraphQLError.js
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
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