locatedError.mjs 964 B

1234567891011121314151617181920212223242526272829303132
  1. import { toError } from '../jsutils/toError.mjs';
  2. import { GraphQLError } from './GraphQLError.mjs';
  3. /**
  4. * Given an arbitrary value, presumably thrown while attempting to execute a
  5. * GraphQL operation, produce a new GraphQLError aware of the location in the
  6. * document responsible for the original Error.
  7. */
  8. export function locatedError(rawOriginalError, nodes, path) {
  9. var _nodes;
  10. const originalError = toError(rawOriginalError); // Note: this uses a brand-check to support GraphQL errors originating from other contexts.
  11. if (isLocatedGraphQLError(originalError)) {
  12. return originalError;
  13. }
  14. return new GraphQLError(originalError.message, {
  15. nodes:
  16. (_nodes = originalError.nodes) !== null && _nodes !== void 0
  17. ? _nodes
  18. : nodes,
  19. source: originalError.source,
  20. positions: originalError.positions,
  21. path,
  22. originalError,
  23. });
  24. }
  25. function isLocatedGraphQLError(error) {
  26. return Array.isArray(error.path);
  27. }