locatedError.js.flow 954 B

12345678910111213141516171819202122232425262728293031
  1. // @flow strict
  2. import { type ASTNode } from '../language/ast';
  3. import { GraphQLError } from './GraphQLError';
  4. /**
  5. * Given an arbitrary Error, presumably thrown while attempting to execute a
  6. * GraphQL operation, produce a new GraphQLError aware of the location in the
  7. * document responsible for the original Error.
  8. */
  9. export function locatedError(
  10. originalError: Error | GraphQLError,
  11. nodes: $ReadOnlyArray<ASTNode>,
  12. path: $ReadOnlyArray<string | number>,
  13. ): GraphQLError {
  14. // Note: this uses a brand-check to support GraphQL errors originating from
  15. // other contexts.
  16. if (originalError && Array.isArray(originalError.path)) {
  17. return (originalError: any);
  18. }
  19. return new GraphQLError(
  20. originalError && originalError.message,
  21. (originalError && (originalError: any).nodes) || nodes,
  22. originalError && (originalError: any).source,
  23. originalError && (originalError: any).positions,
  24. path,
  25. originalError,
  26. );
  27. }