GraphQLError.mjs 12 KB

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