@@ -24,44 +24,44 @@ export class GraphQLError extends Error {
24
24
*
25
25
* Enumerable, and appears in the result of JSON.stringify().
26
26
*/
27
- readonly locations ? : ReadonlyArray < SourceLocation > ;
27
+ readonly locations : ReadonlyArray < SourceLocation > | undefined ;
28
28
29
29
/**
30
30
* An array describing the JSON-path into the execution response which
31
31
* corresponds to this error. Only included for errors during execution.
32
32
*
33
33
* Enumerable, and appears in the result of JSON.stringify().
34
34
*/
35
- readonly path ? : ReadonlyArray < string | number > ;
35
+ readonly path : ReadonlyArray < string | number > | undefined ;
36
36
37
37
/**
38
38
* An array of GraphQL AST Nodes corresponding to this error.
39
39
*/
40
- readonly nodes ? : ReadonlyArray < ASTNode > ;
40
+ readonly nodes : ReadonlyArray < ASTNode > | undefined ;
41
41
42
42
/**
43
43
* The source GraphQL document for the first location of this error.
44
44
*
45
45
* Note that if this Error represents more than one node, the source may not
46
46
* represent nodes after the first node.
47
47
*/
48
- readonly source ? : Source ;
48
+ readonly source : Source | undefined ;
49
49
50
50
/**
51
51
* An array of character offsets within the source GraphQL document
52
52
* which correspond to this error.
53
53
*/
54
- readonly positions ? : ReadonlyArray < number > ;
54
+ readonly positions : ReadonlyArray < number > | undefined ;
55
55
56
56
/**
57
57
* The original error thrown from a field resolver during execution.
58
58
*/
59
- readonly originalError : Maybe < Error > ;
59
+ readonly originalError : Error | undefined ;
60
60
61
61
/**
62
62
* Extension fields to add to the formatted error.
63
63
*/
64
- readonly extensions ? : { [ key : string ] : unknown } ;
64
+ readonly extensions : { [ key : string ] : unknown } | undefined ;
65
65
66
66
constructor (
67
67
message : string ,
@@ -74,8 +74,12 @@ export class GraphQLError extends Error {
74
74
) {
75
75
super ( message ) ;
76
76
77
+ this . name = 'GraphQLError' ;
78
+ this . path = path ?? undefined ;
79
+ this . originalError = originalError ?? undefined ;
80
+
77
81
// Compute list of blame nodes.
78
- const _nodes = Array . isArray ( nodes )
82
+ this . nodes = Array . isArray ( nodes )
79
83
? nodes . length !== 0
80
84
? nodes
81
85
: undefined
@@ -84,96 +88,43 @@ export class GraphQLError extends Error {
84
88
: undefined ;
85
89
86
90
// Compute locations in the source for the given nodes/positions.
87
- let _source = source ;
88
- if ( ! _source && _nodes ) {
89
- _source = _nodes [ 0 ] . loc ?. source ;
91
+ this . source = source ?? undefined ;
92
+ if ( ! this . source && this . nodes ) {
93
+ this . source = this . nodes [ 0 ] . loc ?. source ;
90
94
}
91
95
92
- let _positions ;
93
96
if ( positions ) {
94
- _positions = positions ;
95
- } else if ( _nodes ) {
96
- _positions = [ ] ;
97
- for ( const node of _nodes ) {
97
+ this . positions = positions ;
98
+ } else if ( this . nodes ) {
99
+ const positionsFromNodes = [ ] ;
100
+ for ( const node of this . nodes ) {
98
101
if ( node . loc ) {
99
- _positions . push ( node . loc . start ) ;
102
+ positionsFromNodes . push ( node . loc . start ) ;
100
103
}
101
104
}
105
+ this . positions = positionsFromNodes ;
102
106
}
103
- if ( _positions && _positions . length === 0 ) {
104
- _positions = undefined ;
107
+ if ( this . positions && this . positions . length === 0 ) {
108
+ this . positions = undefined ;
105
109
}
106
110
107
- let _locations ;
108
111
if ( positions && source ) {
109
- _locations = positions . map ( ( pos ) => getLocation ( source , pos ) ) ;
110
- } else if ( _nodes ) {
111
- _locations = [ ] ;
112
- for ( const node of _nodes ) {
112
+ this . locations = positions . map ( ( pos ) => getLocation ( source , pos ) ) ;
113
+ } else if ( this . nodes ) {
114
+ const locationsFromNodes = [ ] ;
115
+ for ( const node of this . nodes ) {
113
116
if ( node . loc ) {
114
- _locations . push ( getLocation ( node . loc . source , node . loc . start ) ) ;
117
+ locationsFromNodes . push ( getLocation ( node . loc . source , node . loc . start ) ) ;
115
118
}
116
119
}
120
+ this . locations = locationsFromNodes ;
117
121
}
118
122
119
- let _extensions = extensions ;
120
- if ( _extensions == null && originalError != null ) {
121
- const originalExtensions = originalError . extensions ;
122
- if ( isObjectLike ( originalExtensions ) ) {
123
- _extensions = originalExtensions ;
124
- }
125
- }
126
-
127
- Object . defineProperties ( this , {
128
- name : { value : 'GraphQLError' } ,
129
- message : {
130
- value : message ,
131
- // By being enumerable, JSON.stringify will include `message` in the
132
- // resulting output. This ensures that the simplest possible GraphQL
133
- // service adheres to the spec.
134
- enumerable : true ,
135
- writable : true ,
136
- } ,
137
- locations : {
138
- // Coercing falsy values to undefined ensures they will not be included
139
- // in JSON.stringify() when not provided.
140
- value : _locations ?? undefined ,
141
- // By being enumerable, JSON.stringify will include `locations` in the
142
- // resulting output. This ensures that the simplest possible GraphQL
143
- // service adheres to the spec.
144
- enumerable : _locations != null ,
145
- } ,
146
- path : {
147
- // Coercing falsy values to undefined ensures they will not be included
148
- // in JSON.stringify() when not provided.
149
- value : path ?? undefined ,
150
- // By being enumerable, JSON.stringify will include `path` in the
151
- // resulting output. This ensures that the simplest possible GraphQL
152
- // service adheres to the spec.
153
- enumerable : path != null ,
154
- } ,
155
- nodes : {
156
- value : _nodes ?? undefined ,
157
- } ,
158
- source : {
159
- value : _source ?? undefined ,
160
- } ,
161
- positions : {
162
- value : _positions ?? undefined ,
163
- } ,
164
- originalError : {
165
- value : originalError ,
166
- } ,
167
- extensions : {
168
- // Coercing falsy values to undefined ensures they will not be included
169
- // in JSON.stringify() when not provided.
170
- value : _extensions ?? undefined ,
171
- // By being enumerable, JSON.stringify will include `path` in the
172
- // resulting output. This ensures that the simplest possible GraphQL
173
- // service adheres to the spec.
174
- enumerable : _extensions != null ,
175
- } ,
176
- } ) ;
123
+ const originalExtensions = isObjectLike ( originalError ?. extensions )
124
+ ? originalError ?. extensions
125
+ : undefined ;
126
+ // TODO: merge `extensions` and `originalExtensions`
127
+ this . extensions = extensions ?? originalExtensions ;
177
128
178
129
// Include (non-enumerable) stack trace.
179
130
if ( originalError ?. stack ) {
0 commit comments