GraphQLError.js 12 KB

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