Skip to content

Commit 463f075

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

File tree

2 files changed

+64
-3
lines changed

2 files changed

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

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

0 commit comments

Comments
 (0)