GraphQLError.mjs 4.9 KB

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