Skip to content

Commit c4ead8a

Browse files
committed
GraphQLError: enumarate only spec prescribed properties
1 parent b936411 commit c4ead8a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/error/GraphQLError.ts

+11
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ export class GraphQLError extends Error {
117117
: undefined;
118118
this.extensions = extensions ?? originalExtensions ?? Object.create(null);
119119

120+
// Only properties prescribed by the spec should be enumeratable.
121+
// Keep the rest as non-enumeratable.
122+
Object.defineProperties(this, {
123+
message: { enumerable: true },
124+
name: { enumerable: false },
125+
nodes: { enumerable: false },
126+
source: { enumerable: false },
127+
positions: { enumerable: false },
128+
originalError: { enumerable: false },
129+
});
130+
120131
// Include (non-enumerable) stack trace.
121132
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
122133
if (originalError?.stack) {

src/error/__tests__/GraphQLError-test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ describe('GraphQLError', () => {
3939
expect(e.stack).to.be.a('string');
4040
});
4141

42+
it('enumerate only properties prescribed by the spec', () => {
43+
const e = new GraphQLError(
44+
'msg' /* message */,
45+
[fieldNode] /* nodes */,
46+
source /* source */,
47+
[1, 2, 3] /* positions */,
48+
['a', 'b', 'c'] /* path */,
49+
new Error('test') /* originalError */,
50+
{ foo: 'bar' } /* extensions */,
51+
);
52+
53+
expect(Object.keys(e)).to.deep.equal([
54+
'message',
55+
'path',
56+
'locations',
57+
'extensions',
58+
]);
59+
});
60+
4261
it('uses the stack of an original error', () => {
4362
const original = new Error('original');
4463
const e = new GraphQLError(

0 commit comments

Comments
 (0)