GraphQLError.mjs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import { isObjectLike } from '../jsutils/isObjectLike.mjs';
  2. import { getLocation } from '../language/location.mjs';
  3. import {
  4. printLocation,
  5. printSourceLocation,
  6. } from '../language/printLocation.mjs';
  7. function toNormalizedOptions(args) {
  8. const firstArg = args[0];
  9. if (firstArg == null || 'kind' in firstArg || 'length' in firstArg) {
  10. return {
  11. nodes: firstArg,
  12. source: args[1],
  13. positions: args[2],
  14. path: args[3],
  15. originalError: args[4],
  16. extensions: args[5],
  17. };
  18. }
  19. return firstArg;
  20. }
  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 class GraphQLError extends Error {
  28. /**
  29. * An array of `{ line, column }` locations within the source GraphQL document
  30. * which correspond to this error.
  31. *
  32. * Errors during validation often contain multiple locations, for example to
  33. * point out two things with the same name. Errors during execution include a
  34. * single location, the field which produced the error.
  35. *
  36. * Enumerable, and appears in the result of JSON.stringify().
  37. */
  38. /**
  39. * An array describing the JSON-path into the execution response which
  40. * corresponds to this error. Only included for errors during execution.
  41. *
  42. * Enumerable, and appears in the result of JSON.stringify().
  43. */
  44. /**
  45. * An array of GraphQL AST Nodes corresponding to this error.
  46. */
  47. /**
  48. * The source GraphQL document for the first location of this error.
  49. *
  50. * Note that if this Error represents more than one node, the source may not
  51. * represent nodes after the first node.
  52. */
  53. /**
  54. * An array of character offsets within the source GraphQL document
  55. * which correspond to this error.
  56. */
  57. /**
  58. * The original error thrown from a field resolver during execution.
  59. */
  60. /**
  61. * Extension fields to add to the formatted error.
  62. */
  63. /**
  64. * @deprecated Please use the `GraphQLErrorOptions` constructor overload instead.
  65. */
  66. constructor(message, ...rawArgs) {
  67. var _this$nodes, _nodeLocations$, _ref;
  68. const { nodes, source, positions, path, originalError, extensions } =
  69. toNormalizedOptions(rawArgs);
  70. super(message);
  71. this.name = 'GraphQLError';
  72. this.path = path !== null && path !== void 0 ? path : undefined;
  73. this.originalError =
  74. originalError !== null && originalError !== void 0
  75. ? originalError
  76. : undefined; // Compute list of blame nodes.
  77. this.nodes = undefinedIfEmpty(
  78. Array.isArray(nodes) ? nodes : nodes ? [nodes] : undefined,
  79. );
  80. const nodeLocations = undefinedIfEmpty(
  81. (_this$nodes = this.nodes) === null || _this$nodes === void 0
  82. ? void 0
  83. : _this$nodes.map((node) => node.loc).filter((loc) => loc != null),
  84. ); // Compute locations in the source for the given nodes/positions.
  85. this.source =
  86. source !== null && source !== void 0
  87. ? source
  88. : nodeLocations === null || nodeLocations === void 0
  89. ? void 0
  90. : (_nodeLocations$ = nodeLocations[0]) === null ||
  91. _nodeLocations$ === void 0
  92. ? void 0
  93. : _nodeLocations$.source;
  94. this.positions =
  95. positions !== null && positions !== void 0
  96. ? positions
  97. : nodeLocations === null || nodeLocations === void 0
  98. ? void 0
  99. : nodeLocations.map((loc) => loc.start);
  100. this.locations =
  101. positions && source
  102. ? positions.map((pos) => getLocation(source, pos))
  103. : nodeLocations === null || nodeLocations === void 0
  104. ? void 0
  105. : nodeLocations.map((loc) => getLocation(loc.source, loc.start));
  106. const originalExtensions = isObjectLike(
  107. originalError === null || originalError === void 0
  108. ? void 0
  109. : originalError.extensions,
  110. )
  111. ? originalError === null || originalError === void 0
  112. ? void 0
  113. : originalError.extensions
  114. : undefined;
  115. this.extensions =
  116. (_ref =
  117. extensions !== null && extensions !== void 0
  118. ? extensions
  119. : originalExtensions) !== null && _ref !== void 0
  120. ? _ref
  121. : Object.create(null); // Only properties prescribed by the spec should be enumerable.
  122. // Keep the rest as non-enumerable.
  123. Object.defineProperties(this, {
  124. message: {
  125. writable: true,
  126. enumerable: true,
  127. },
  128. name: {
  129. enumerable: false,
  130. },
  131. nodes: {
  132. enumerable: false,
  133. },
  134. source: {
  135. enumerable: false,
  136. },
  137. positions: {
  138. enumerable: false,
  139. },
  140. originalError: {
  141. enumerable: false,
  142. },
  143. }); // Include (non-enumerable) stack trace.
  144. /* c8 ignore start */
  145. // FIXME: https://github.com/graphql/graphql-js/issues/2317
  146. if (
  147. originalError !== null &&
  148. originalError !== void 0 &&
  149. originalError.stack
  150. ) {
  151. Object.defineProperty(this, 'stack', {
  152. value: originalError.stack,
  153. writable: true,
  154. configurable: true,
  155. });
  156. } else if (Error.captureStackTrace) {
  157. Error.captureStackTrace(this, GraphQLError);
  158. } else {
  159. Object.defineProperty(this, 'stack', {
  160. value: Error().stack,
  161. writable: true,
  162. configurable: true,
  163. });
  164. }
  165. /* c8 ignore stop */
  166. }
  167. get [Symbol.toStringTag]() {
  168. return 'GraphQLError';
  169. }
  170. toString() {
  171. let output = this.message;
  172. if (this.nodes) {
  173. for (const node of this.nodes) {
  174. if (node.loc) {
  175. output += '\n\n' + printLocation(node.loc);
  176. }
  177. }
  178. } else if (this.source && this.locations) {
  179. for (const location of this.locations) {
  180. output += '\n\n' + printSourceLocation(this.source, location);
  181. }
  182. }
  183. return output;
  184. }
  185. toJSON() {
  186. const formattedError = {
  187. message: this.message,
  188. };
  189. if (this.locations != null) {
  190. formattedError.locations = this.locations;
  191. }
  192. if (this.path != null) {
  193. formattedError.path = this.path;
  194. }
  195. if (this.extensions != null && Object.keys(this.extensions).length > 0) {
  196. formattedError.extensions = this.extensions;
  197. }
  198. return formattedError;
  199. }
  200. }
  201. function undefinedIfEmpty(array) {
  202. return array === undefined || array.length === 0 ? undefined : array;
  203. }
  204. /**
  205. * See: https://spec.graphql.org/draft/#sec-Errors
  206. */
  207. /**
  208. * Prints a GraphQLError to a string, representing useful location information
  209. * about the error's position in the source.
  210. *
  211. * @deprecated Please use `error.toString` instead. Will be removed in v17
  212. */
  213. export function printError(error) {
  214. return error.toString();
  215. }
  216. /**
  217. * Given a GraphQLError, format it according to the rules described by the
  218. * Response Format, Errors section of the GraphQL Specification.
  219. *
  220. * @deprecated Please use `error.toJSON` instead. Will be removed in v17
  221. */
  222. export function formatError(error) {
  223. return error.toJSON();
  224. }