Skip to content

Commit a1cc39d

Browse files
committed
Allow to extend GraphQL errors with additional properties
1 parent f2d2d24 commit a1cc39d

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

src/error/GraphQLError.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ declare class GraphQLError extends Error {
2626
source?: ?Source,
2727
positions?: ?Array<number>,
2828
path?: ?Array<string | number>,
29-
originalError?: ?Error
29+
originalError?: ?Error,
30+
extensions?: ?{ [key: string]: mixed },
3031
): void;
3132

3233
/**
@@ -76,6 +77,11 @@ declare class GraphQLError extends Error {
7677
* The original error thrown from a field resolver during execution.
7778
*/
7879
originalError: ?Error;
80+
81+
/**
82+
* The original error thrown from a field resolver during execution.
83+
*/
84+
extensions: ?{ [key: string]: mixed };
7985
}
8086

8187
export function GraphQLError( // eslint-disable-line no-redeclare
@@ -84,7 +90,8 @@ export function GraphQLError( // eslint-disable-line no-redeclare
8490
source?: ?Source,
8591
positions?: ?Array<number>,
8692
path?: ?Array<string | number>,
87-
originalError?: ?Error
93+
originalError?: ?Error,
94+
extensions?: ?{ [key: string]: mixed }
8895
) {
8996
// Compute locations in the source for the given nodes/positions.
9097
let _source = source;
@@ -146,7 +153,10 @@ export function GraphQLError( // eslint-disable-line no-redeclare
146153
},
147154
originalError: {
148155
value: originalError
149-
}
156+
},
157+
extensions: {
158+
value: extensions
159+
},
150160
});
151161

152162
// Include (non-enumerable) stack trace.

src/error/__tests__/GraphQLError-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,23 @@ describe('GraphQLError', () => {
139139
});
140140
});
141141

142+
it('default error formatter includes extension fields', () => {
143+
const e = new GraphQLError(
144+
'msg',
145+
null,
146+
null,
147+
null,
148+
null,
149+
null,
150+
{ foo: 'bar' }
151+
);
152+
153+
expect(formatError(e)).to.deep.equal({
154+
message: 'msg',
155+
locations: undefined,
156+
path: undefined,
157+
foo: 'bar'
158+
});
159+
});
160+
142161
});

src/error/formatError.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type { GraphQLError } from './GraphQLError';
1919
export function formatError(error: GraphQLError): GraphQLFormattedError {
2020
invariant(error, 'Received null or undefined error.');
2121
return {
22+
...error.extensions,
2223
message: error.message,
2324
locations: error.locations,
2425
path: error.path

src/error/locatedError.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export function locatedError(
3636
originalError && (originalError: any).source,
3737
originalError && (originalError: any).positions,
3838
path,
39-
originalError
39+
originalError,
40+
originalError && (originalError: any).extensions,
4041
);
4142
}

0 commit comments

Comments
 (0)