1
- // FIXME:
2
- // flowlint uninitialized-instance-property:off
3
-
4
1
import isObjectLike from '../jsutils/isObjectLike' ;
5
2
import { SYMBOL_TO_STRING_TAG } from '../polyfills/symbols' ;
6
3
@@ -17,15 +14,6 @@ import { printLocation, printSourceLocation } from '../language/printLocation';
17
14
* GraphQL document and/or execution result that correspond to the Error.
18
15
*/
19
16
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
-
29
17
/**
30
18
* An array of { line, column } locations within the source GraphQL document
31
19
* which correspond to this error.
@@ -68,7 +56,7 @@ export class GraphQLError extends Error {
68
56
/**
69
57
* The original error thrown from a field resolver during execution.
70
58
*/
71
- + originalError : ? Error ;
59
+ + originalError : Error | void ;
72
60
73
61
/**
74
62
* Extension fields to add to the formatted error.
@@ -86,85 +74,58 @@ export class GraphQLError extends Error {
86
74
) {
87
75
super ( message ) ;
88
76
77
+ this . name = 'GraphQLError' ;
78
+ this . path = path ?? undefined ;
79
+ this . originalError = originalError ?? undefined ;
80
+
89
81
// Compute list of blame nodes.
90
- const _nodes = undefinedIfEmpty (
82
+ this . nodes = undefinedIfEmpty (
91
83
Array . isArray ( nodes ) ? nodes : nodes ? [ nodes ] : undefined ,
92
84
) ;
93
85
94
86
let nodeLocations = [ ] ;
95
- for ( const { loc } of _nodes ?? [ ] ) {
87
+ for ( const { loc } of this . nodes ?? [ ] ) {
96
88
if ( loc != null ) {
97
89
nodeLocations . push ( loc ) ;
98
90
}
99
91
}
100
92
nodeLocations = undefinedIfEmpty ( nodeLocations ) ;
101
93
102
94
// Compute locations in the source for the given nodes/positions.
103
- const _source = source ?? nodeLocations ?. [ 0 ] . source ;
95
+ this . source = source ?? nodeLocations ?. [ 0 ] . source ;
104
96
105
- const _positions = positions ?? nodeLocations ?. map ( ( loc ) => loc . start ) ;
97
+ this . positions = positions ?? nodeLocations ?. map ( ( loc ) => loc . start ) ;
106
98
107
- const _locations =
99
+ this . locations =
108
100
positions && source
109
101
? positions . map ( ( pos ) => getLocation ( source , pos ) )
110
102
: nodeLocations ?. map ( ( loc ) => getLocation ( loc . source , loc . start ) ) ;
111
103
112
- let _extensions = extensions ?? undefined ;
104
+ this . extensions = extensions ?? undefined ;
113
105
114
106
const originalExtensions = originalError ?. extensions ;
115
107
if ( isObjectLike ( originalExtensions ) ) {
116
- _extensions = originalExtensions ;
108
+ this . extensions = { ... originalExtensions } ;
117
109
}
118
110
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.
119
113
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 } ,
129
115
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 ,
137
117
} ,
138
118
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 ,
158
120
} ,
159
121
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 ,
167
123
} ,
124
+ name : { enumerable : false } ,
125
+ nodes : { enumerable : false } ,
126
+ source : { enumerable : false } ,
127
+ positions : { enumerable : false } ,
128
+ originalError : { enumerable : false } ,
168
129
} ) ;
169
130
170
131
// Include (non-enumerable) stack trace.
0 commit comments