Skip to content

Commit a69b8b4

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

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/error/GraphQLError.ts

Lines changed: 11 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ 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(['message', 'path', 'locations', 'extensions']);
54+
});
55+
4256
it('uses the stack of an original error', () => {
4357
const original = new Error('original');
4458
const e = new GraphQLError(

0 commit comments

Comments
 (0)