Skip to content

Commit d8ca570

Browse files
GraphQLError: major refactoring to be more in line with v16 (#3333)
1 parent 8ba5c56 commit d8ca570

File tree

2 files changed

+24
-72
lines changed

2 files changed

+24
-72
lines changed

src/error/GraphQLError.d.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,6 @@ export class GraphQLError extends Error {
3434
extensions?: Maybe<{ [key: string]: GraphQLErrorExtensions }>,
3535
);
3636

37-
/**
38-
* A message describing the Error for debugging purposes.
39-
*
40-
* Enumerable, and appears in the result of JSON.stringify().
41-
*
42-
* Note: should be treated as readonly, despite invariant usage.
43-
*/
44-
message: string;
45-
4637
/**
4738
* An array of { line, column } locations within the source GraphQL document
4839
* which correspond to this error.
@@ -85,7 +76,7 @@ export class GraphQLError extends Error {
8576
/**
8677
* The original error thrown from a field resolver during execution.
8778
*/
88-
readonly originalError: Maybe<Error>;
79+
readonly originalError: Error | undefined;
8980

9081
/**
9182
* Extension fields to add to the formatted error.

src/error/GraphQLError.js

Lines changed: 23 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// FIXME:
2-
// flowlint uninitialized-instance-property:off
3-
41
import isObjectLike from '../jsutils/isObjectLike';
52
import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols';
63

@@ -17,15 +14,6 @@ import { printLocation, printSourceLocation } from '../language/printLocation';
1714
* GraphQL document and/or execution result that correspond to the Error.
1815
*/
1916
export class GraphQLError extends Error {
20-
/**
21-
* A message describing the Error for debugging purposes.
22-
*
23-
* Enumerable, and appears in the result of JSON.stringify().
24-
*
25-
* Note: should be treated as readonly, despite invariant usage.
26-
*/
27-
message: string;
28-
2917
/**
3018
* An array of { line, column } locations within the source GraphQL document
3119
* which correspond to this error.
@@ -68,7 +56,7 @@ export class GraphQLError extends Error {
6856
/**
6957
* The original error thrown from a field resolver during execution.
7058
*/
71-
+originalError: ?Error;
59+
+originalError: Error | void;
7260

7361
/**
7462
* Extension fields to add to the formatted error.
@@ -86,85 +74,58 @@ export class GraphQLError extends Error {
8674
) {
8775
super(message);
8876

77+
this.name = 'GraphQLError';
78+
this.path = path ?? undefined;
79+
this.originalError = originalError ?? undefined;
80+
8981
// Compute list of blame nodes.
90-
const _nodes = undefinedIfEmpty(
82+
this.nodes = undefinedIfEmpty(
9183
Array.isArray(nodes) ? nodes : nodes ? [nodes] : undefined,
9284
);
9385

9486
let nodeLocations = [];
95-
for (const { loc } of _nodes ?? []) {
87+
for (const { loc } of this.nodes ?? []) {
9688
if (loc != null) {
9789
nodeLocations.push(loc);
9890
}
9991
}
10092
nodeLocations = undefinedIfEmpty(nodeLocations);
10193

10294
// Compute locations in the source for the given nodes/positions.
103-
const _source = source ?? nodeLocations?.[0].source;
95+
this.source = source ?? nodeLocations?.[0].source;
10496

105-
const _positions = positions ?? nodeLocations?.map((loc) => loc.start);
97+
this.positions = positions ?? nodeLocations?.map((loc) => loc.start);
10698

107-
const _locations =
99+
this.locations =
108100
positions && source
109101
? positions.map((pos) => getLocation(source, pos))
110102
: nodeLocations?.map((loc) => getLocation(loc.source, loc.start));
111103

112-
let _extensions = extensions ?? undefined;
104+
this.extensions = extensions ?? undefined;
113105

114106
const originalExtensions = originalError?.extensions;
115107
if (isObjectLike(originalExtensions)) {
116-
_extensions = originalExtensions;
108+
this.extensions = { ...originalExtensions };
117109
}
118110

111+
// By being enumerable, JSON.stringify will include bellow properties in the resulting output.
112+
// This ensures that the simplest possible GraphQL service adheres to the spec.
119113
Object.defineProperties((this: any), {
120-
name: { value: 'GraphQLError' },
121-
message: {
122-
value: message,
123-
// By being enumerable, JSON.stringify will include `message` in the
124-
// resulting output. This ensures that the simplest possible GraphQL
125-
// service adheres to the spec.
126-
enumerable: true,
127-
writable: true,
128-
},
114+
message: { enumerable: true },
129115
locations: {
130-
// Coercing falsy values to undefined ensures they will not be included
131-
// in JSON.stringify() when not provided.
132-
value: _locations ?? undefined,
133-
// By being enumerable, JSON.stringify will include `locations` in the
134-
// resulting output. This ensures that the simplest possible GraphQL
135-
// service adheres to the spec.
136-
enumerable: _locations != null,
116+
enumerable: this.locations != null,
137117
},
138118
path: {
139-
// Coercing falsy values to undefined ensures they will not be included
140-
// in JSON.stringify() when not provided.
141-
value: path ?? undefined,
142-
// By being enumerable, JSON.stringify will include `path` in the
143-
// resulting output. This ensures that the simplest possible GraphQL
144-
// service adheres to the spec.
145-
enumerable: path != null,
146-
},
147-
nodes: {
148-
value: _nodes ?? undefined,
149-
},
150-
source: {
151-
value: _source ?? undefined,
152-
},
153-
positions: {
154-
value: _positions ?? undefined,
155-
},
156-
originalError: {
157-
value: originalError,
119+
enumerable: this.path != null,
158120
},
159121
extensions: {
160-
// Coercing falsy values to undefined ensures they will not be included
161-
// in JSON.stringify() when not provided.
162-
value: _extensions ?? undefined,
163-
// By being enumerable, JSON.stringify will include `path` in the
164-
// resulting output. This ensures that the simplest possible GraphQL
165-
// service adheres to the spec.
166-
enumerable: _extensions != null,
122+
enumerable: this.extensions != null,
167123
},
124+
name: { enumerable: false },
125+
nodes: { enumerable: false },
126+
source: { enumerable: false },
127+
positions: { enumerable: false },
128+
originalError: { enumerable: false },
168129
});
169130

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

0 commit comments

Comments
 (0)