GraphQLError.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.GraphQLError = GraphQLError;
  6. exports.printError = printError;
  7. var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike"));
  8. var _location = require("../language/location");
  9. var _printLocation = require("../language/printLocation");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function GraphQLError( // eslint-disable-line no-redeclare
  12. message, nodes, source, positions, path, originalError, extensions) {
  13. // Compute list of blame nodes.
  14. var _nodes = Array.isArray(nodes) ? nodes.length !== 0 ? nodes : undefined : nodes ? [nodes] : undefined; // Compute locations in the source for the given nodes/positions.
  15. var _source = source;
  16. if (!_source && _nodes) {
  17. var node = _nodes[0];
  18. _source = node && node.loc && node.loc.source;
  19. }
  20. var _positions = positions;
  21. if (!_positions && _nodes) {
  22. _positions = _nodes.reduce(function (list, node) {
  23. if (node.loc) {
  24. list.push(node.loc.start);
  25. }
  26. return list;
  27. }, []);
  28. }
  29. if (_positions && _positions.length === 0) {
  30. _positions = undefined;
  31. }
  32. var _locations;
  33. if (positions && source) {
  34. _locations = positions.map(function (pos) {
  35. return (0, _location.getLocation)(source, pos);
  36. });
  37. } else if (_nodes) {
  38. _locations = _nodes.reduce(function (list, node) {
  39. if (node.loc) {
  40. list.push((0, _location.getLocation)(node.loc.source, node.loc.start));
  41. }
  42. return list;
  43. }, []);
  44. }
  45. var _extensions = extensions;
  46. if (_extensions == null && originalError != null) {
  47. var originalExtensions = originalError.extensions;
  48. if ((0, _isObjectLike.default)(originalExtensions)) {
  49. _extensions = originalExtensions;
  50. }
  51. }
  52. Object.defineProperties(this, {
  53. message: {
  54. value: message,
  55. // By being enumerable, JSON.stringify will include `message` in the
  56. // resulting output. This ensures that the simplest possible GraphQL
  57. // service adheres to the spec.
  58. enumerable: true,
  59. writable: true
  60. },
  61. locations: {
  62. // Coercing falsey values to undefined ensures they will not be included
  63. // in JSON.stringify() when not provided.
  64. value: _locations || undefined,
  65. // By being enumerable, JSON.stringify will include `locations` in the
  66. // resulting output. This ensures that the simplest possible GraphQL
  67. // service adheres to the spec.
  68. enumerable: Boolean(_locations)
  69. },
  70. path: {
  71. // Coercing falsey values to undefined ensures they will not be included
  72. // in JSON.stringify() when not provided.
  73. value: path || undefined,
  74. // By being enumerable, JSON.stringify will include `path` in the
  75. // resulting output. This ensures that the simplest possible GraphQL
  76. // service adheres to the spec.
  77. enumerable: Boolean(path)
  78. },
  79. nodes: {
  80. value: _nodes || undefined
  81. },
  82. source: {
  83. value: _source || undefined
  84. },
  85. positions: {
  86. value: _positions || undefined
  87. },
  88. originalError: {
  89. value: originalError
  90. },
  91. extensions: {
  92. // Coercing falsey values to undefined ensures they will not be included
  93. // in JSON.stringify() when not provided.
  94. value: _extensions || undefined,
  95. // By being enumerable, JSON.stringify will include `path` in the
  96. // resulting output. This ensures that the simplest possible GraphQL
  97. // service adheres to the spec.
  98. enumerable: Boolean(_extensions)
  99. }
  100. }); // Include (non-enumerable) stack trace.
  101. if (originalError && originalError.stack) {
  102. Object.defineProperty(this, 'stack', {
  103. value: originalError.stack,
  104. writable: true,
  105. configurable: true
  106. });
  107. } else if (Error.captureStackTrace) {
  108. Error.captureStackTrace(this, GraphQLError);
  109. } else {
  110. Object.defineProperty(this, 'stack', {
  111. value: Error().stack,
  112. writable: true,
  113. configurable: true
  114. });
  115. }
  116. }
  117. GraphQLError.prototype = Object.create(Error.prototype, {
  118. constructor: {
  119. value: GraphQLError
  120. },
  121. name: {
  122. value: 'GraphQLError'
  123. },
  124. toString: {
  125. value: function toString() {
  126. return printError(this);
  127. }
  128. }
  129. });
  130. /**
  131. * Prints a GraphQLError to a string, representing useful location information
  132. * about the error's position in the source.
  133. */
  134. function printError(error) {
  135. var output = error.message;
  136. if (error.nodes) {
  137. for (var _i2 = 0, _error$nodes2 = error.nodes; _i2 < _error$nodes2.length; _i2++) {
  138. var node = _error$nodes2[_i2];
  139. if (node.loc) {
  140. output += '\n\n' + (0, _printLocation.printLocation)(node.loc);
  141. }
  142. }
  143. } else if (error.source && error.locations) {
  144. for (var _i4 = 0, _error$locations2 = error.locations; _i4 < _error$locations2.length; _i4++) {
  145. var location = _error$locations2[_i4];
  146. output += '\n\n' + (0, _printLocation.printSourceLocation)(error.source, location);
  147. }
  148. }
  149. return output;
  150. }