Skip to content
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
14 changes: 14 additions & 0 deletions src/error/GraphQLError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ export class GraphQLError extends Error {
: undefined;
this.extensions = extensions ?? originalExtensions ?? Object.create(null);

// Only properties prescribed by the spec should be enumerable.
// Keep the rest as non-enumerable.
Object.defineProperties(this, {
message: {
writable: true,
enumerable: true,
},
name: { enumerable: false },
nodes: { enumerable: false },
source: { enumerable: false },
positions: { enumerable: false },
originalError: { enumerable: false },
});

// Include (non-enumerable) stack trace.
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
if (originalError?.stack) {
Expand Down
19 changes: 19 additions & 0 deletions src/error/__tests__/GraphQLError-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ describe('GraphQLError', () => {
expect(e.stack).to.be.a('string');
});

it('enumerate only properties prescribed by the spec', () => {
const e = new GraphQLError(
'msg' /* message */,
[fieldNode] /* nodes */,
source /* source */,
[1, 2, 3] /* positions */,
['a', 'b', 'c'] /* path */,
new Error('test') /* originalError */,
{ foo: 'bar' } /* extensions */,
);

expect(Object.keys(e)).to.deep.equal([
'message',
'path',
'locations',
'extensions',
]);
});

it('uses the stack of an original error', () => {
const original = new Error('original');
const e = new GraphQLError(
Expand Down