Skip to content

Commit f6329e9

Browse files
committed
GraphQLError: Fixed a regression for extensions before the refactoring
1 parent 7dc29fd commit f6329e9

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

src/error/GraphQLError.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ export class GraphQLError extends Error {
102102

103103
this.path = path ?? undefined;
104104

105-
this.extensions = extensions ?? {};
106-
107105
const originalExtensions = originalError?.extensions;
108-
if (isObjectLike(originalExtensions)) {
106+
107+
if (extensions == null && isObjectLike(originalExtensions)) {
109108
this.extensions = { ...originalExtensions };
109+
} else {
110+
this.extensions = extensions ?? {};
110111
}
111112

112113
// By being enumerable, JSON.stringify will include bellow properties in the resulting output.

src/error/__tests__/GraphQLError-test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,70 @@ describe('GraphQLError', () => {
164164
}
165165
`);
166166
});
167+
168+
it('uses the provided extensions when original extensions are undefined', () => {
169+
const original = new Error('original');
170+
console.log('Creating error 1');
171+
const graphqlError = new GraphQLError(
172+
'msg',
173+
null,
174+
null,
175+
null,
176+
null,
177+
original,
178+
{
179+
someKey: 'someValue',
180+
},
181+
);
182+
console.log('Creating error 2');
183+
184+
const e = new GraphQLError(
185+
graphqlError.message,
186+
null,
187+
null,
188+
null,
189+
null,
190+
graphqlError,
191+
{ correlationId: '123-echo-tango-delta' },
192+
);
193+
194+
expect(JSON.stringify(e.extensions, null, 2) + '\n').to.equal(dedent`
195+
{
196+
"correlationId": "123-echo-tango-delta"
197+
}
198+
`);
199+
});
200+
201+
it('uses the provided extensions when original extensions are empty object', () => {
202+
const original = new Error('original');
203+
const graphqlError = new GraphQLError(
204+
'msg',
205+
null,
206+
null,
207+
null,
208+
null,
209+
original,
210+
{},
211+
);
212+
expect(graphqlError.extensions).to.eql({});
213+
214+
const e = new GraphQLError(
215+
graphqlError.message,
216+
null,
217+
null,
218+
null,
219+
null,
220+
graphqlError,
221+
{ correlationId: '123-echo-tango-delta' },
222+
);
223+
expect(e.originalError.extensions).to.eql({});
224+
225+
expect(JSON.stringify(e.extensions, null, 2) + '\n').to.equal(dedent`
226+
{
227+
"correlationId": "123-echo-tango-delta"
228+
}
229+
`);
230+
});
167231
});
168232

169233
describe('printError', () => {

0 commit comments

Comments
 (0)