UNPKG

12.2 kBJavaScriptView Raw
1function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
2
3function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
4
5function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
6
7function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
8
9function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
10
11function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
12
13function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
14
15function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
16
17function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
18
19function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
20
21function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
22
23function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
24
25function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
26
27function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
28
29// FIXME:
30// flowlint uninitialized-instance-property:off
31import isObjectLike from "../jsutils/isObjectLike.mjs";
32import { SYMBOL_TO_STRING_TAG } from "../polyfills/symbols.mjs";
33import { getLocation } from "../language/location.mjs";
34import { printLocation, printSourceLocation } from "../language/printLocation.mjs";
35/**
36 * A GraphQLError describes an Error found during the parse, validate, or
37 * execute phases of performing a GraphQL operation. In addition to a message
38 * and stack trace, it also includes information about the locations in a
39 * GraphQL document and/or execution result that correspond to the Error.
40 */
41
42export var GraphQLError = /*#__PURE__*/function (_Error) {
43 _inherits(GraphQLError, _Error);
44
45 var _super = _createSuper(GraphQLError);
46
47 /**
48 * A message describing the Error for debugging purposes.
49 *
50 * Enumerable, and appears in the result of JSON.stringify().
51 *
52 * Note: should be treated as readonly, despite invariant usage.
53 */
54
55 /**
56 * An array of { line, column } locations within the source GraphQL document
57 * which correspond to this error.
58 *
59 * Errors during validation often contain multiple locations, for example to
60 * point out two things with the same name. Errors during execution include a
61 * single location, the field which produced the error.
62 *
63 * Enumerable, and appears in the result of JSON.stringify().
64 */
65
66 /**
67 * An array describing the JSON-path into the execution response which
68 * corresponds to this error. Only included for errors during execution.
69 *
70 * Enumerable, and appears in the result of JSON.stringify().
71 */
72
73 /**
74 * An array of GraphQL AST Nodes corresponding to this error.
75 */
76
77 /**
78 * The source GraphQL document for the first location of this error.
79 *
80 * Note that if this Error represents more than one node, the source may not
81 * represent nodes after the first node.
82 */
83
84 /**
85 * An array of character offsets within the source GraphQL document
86 * which correspond to this error.
87 */
88
89 /**
90 * The original error thrown from a field resolver during execution.
91 */
92
93 /**
94 * Extension fields to add to the formatted error.
95 */
96 function GraphQLError(message, nodes, source, positions, path, originalError, extensions) {
97 var _locations2, _source2, _positions2, _extensions2;
98
99 var _this;
100
101 _classCallCheck(this, GraphQLError);
102
103 _this = _super.call(this, message); // Compute list of blame nodes.
104
105 var _nodes = Array.isArray(nodes) ? nodes.length !== 0 ? nodes : undefined : nodes ? [nodes] : undefined; // Compute locations in the source for the given nodes/positions.
106
107
108 var _source = source;
109
110 if (!_source && _nodes) {
111 var _nodes$0$loc;
112
113 _source = (_nodes$0$loc = _nodes[0].loc) === null || _nodes$0$loc === void 0 ? void 0 : _nodes$0$loc.source;
114 }
115
116 var _positions = positions;
117
118 if (!_positions && _nodes) {
119 _positions = _nodes.reduce(function (list, node) {
120 if (node.loc) {
121 list.push(node.loc.start);
122 }
123
124 return list;
125 }, []);
126 }
127
128 if (_positions && _positions.length === 0) {
129 _positions = undefined;
130 }
131
132 var _locations;
133
134 if (positions && source) {
135 _locations = positions.map(function (pos) {
136 return getLocation(source, pos);
137 });
138 } else if (_nodes) {
139 _locations = _nodes.reduce(function (list, node) {
140 if (node.loc) {
141 list.push(getLocation(node.loc.source, node.loc.start));
142 }
143
144 return list;
145 }, []);
146 }
147
148 var _extensions = extensions;
149
150 if (_extensions == null && originalError != null) {
151 var originalExtensions = originalError.extensions;
152
153 if (isObjectLike(originalExtensions)) {
154 _extensions = originalExtensions;
155 }
156 }
157
158 Object.defineProperties(_assertThisInitialized(_this), {
159 name: {
160 value: 'GraphQLError'
161 },
162 message: {
163 value: message,
164 // By being enumerable, JSON.stringify will include `message` in the
165 // resulting output. This ensures that the simplest possible GraphQL
166 // service adheres to the spec.
167 enumerable: true,
168 writable: true
169 },
170 locations: {
171 // Coercing falsy values to undefined ensures they will not be included
172 // in JSON.stringify() when not provided.
173 value: (_locations2 = _locations) !== null && _locations2 !== void 0 ? _locations2 : undefined,
174 // By being enumerable, JSON.stringify will include `locations` in the
175 // resulting output. This ensures that the simplest possible GraphQL
176 // service adheres to the spec.
177 enumerable: _locations != null
178 },
179 path: {
180 // Coercing falsy values to undefined ensures they will not be included
181 // in JSON.stringify() when not provided.
182 value: path !== null && path !== void 0 ? path : undefined,
183 // By being enumerable, JSON.stringify will include `path` in the
184 // resulting output. This ensures that the simplest possible GraphQL
185 // service adheres to the spec.
186 enumerable: path != null
187 },
188 nodes: {
189 value: _nodes !== null && _nodes !== void 0 ? _nodes : undefined
190 },
191 source: {
192 value: (_source2 = _source) !== null && _source2 !== void 0 ? _source2 : undefined
193 },
194 positions: {
195 value: (_positions2 = _positions) !== null && _positions2 !== void 0 ? _positions2 : undefined
196 },
197 originalError: {
198 value: originalError
199 },
200 extensions: {
201 // Coercing falsy values to undefined ensures they will not be included
202 // in JSON.stringify() when not provided.
203 value: (_extensions2 = _extensions) !== null && _extensions2 !== void 0 ? _extensions2 : undefined,
204 // By being enumerable, JSON.stringify will include `path` in the
205 // resulting output. This ensures that the simplest possible GraphQL
206 // service adheres to the spec.
207 enumerable: _extensions != null
208 }
209 }); // Include (non-enumerable) stack trace.
210
211 if (originalError !== null && originalError !== void 0 && originalError.stack) {
212 Object.defineProperty(_assertThisInitialized(_this), 'stack', {
213 value: originalError.stack,
214 writable: true,
215 configurable: true
216 });
217 return _possibleConstructorReturn(_this);
218 } // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
219
220
221 if (Error.captureStackTrace) {
222 Error.captureStackTrace(_assertThisInitialized(_this), GraphQLError);
223 } else {
224 Object.defineProperty(_assertThisInitialized(_this), 'stack', {
225 value: Error().stack,
226 writable: true,
227 configurable: true
228 });
229 }
230
231 return _this;
232 }
233
234 _createClass(GraphQLError, [{
235 key: "toString",
236 value: function toString() {
237 return printError(this);
238 } // FIXME: workaround to not break chai comparisons, should be remove in v16
239 // $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
240
241 }, {
242 key: SYMBOL_TO_STRING_TAG,
243 get: function get() {
244 return 'Object';
245 }
246 }]);
247
248 return GraphQLError;
249}( /*#__PURE__*/_wrapNativeSuper(Error));
250/**
251 * Prints a GraphQLError to a string, representing useful location information
252 * about the error's position in the source.
253 */
254
255export function printError(error) {
256 var output = error.message;
257
258 if (error.nodes) {
259 for (var _i2 = 0, _error$nodes2 = error.nodes; _i2 < _error$nodes2.length; _i2++) {
260 var node = _error$nodes2[_i2];
261
262 if (node.loc) {
263 output += '\n\n' + printLocation(node.loc);
264 }
265 }
266 } else if (error.source && error.locations) {
267 for (var _i4 = 0, _error$locations2 = error.locations; _i4 < _error$locations2.length; _i4++) {
268 var location = _error$locations2[_i4];
269 output += '\n\n' + printSourceLocation(error.source, location);
270 }
271 }
272
273 return output;
274}