UNPKG

graphql

Version:

A Query Language and Runtime which can target any service.

1 lines 2.63 kB
{"version":3,"file":"locatedError.js","sourceRoot":"","sources":["../../src/error/locatedError.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,+BAA8B;AAIhD,OAAO,EAAE,YAAY,EAAE,2BAA0B;AAwBjD,MAAM,UAAU,YAAY,CAC1B,gBAAyB,EACzB,KAA0D,EAC1D,IAA4C;IAE5C,MAAM,aAAa,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAGhD,IAAI,qBAAqB,CAAC,aAAa,CAAC,EAAE,CAAC;QACzC,OAAO,aAAa,CAAC;IACvB,CAAC;IAED,OAAO,IAAI,YAAY,CAAC,aAAa,CAAC,OAAO,EAAE;QAC7C,KAAK,EAAG,aAA8B,CAAC,KAAK,IAAI,KAAK;QACrD,MAAM,EAAG,aAA8B,CAAC,MAAM;QAC9C,SAAS,EAAG,aAA8B,CAAC,SAAS;QACpD,IAAI;QACJ,aAAa;KACd,CAAC,CAAC;AACL,CAAC;AAED,SAAS,qBAAqB,CAAC,KAAU;IACvC,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC","sourcesContent":["/** @category Errors */\n\nimport type { Maybe } from '../jsutils/Maybe.ts';\nimport { toError } from '../jsutils/toError.ts';\n\nimport type { ASTNode } from '../language/ast.ts';\n\nimport { GraphQLError } from './GraphQLError.ts';\n\n/**\n * Given an arbitrary value, presumably thrown while attempting to execute a\n * GraphQL operation, produce a new GraphQLError aware of the location in the\n * document responsible for the original Error.\n * @param rawOriginalError - The original error value to wrap.\n * @param nodes - The AST nodes associated with the error.\n * @param path - The response path associated with the error.\n * @returns The GraphQL error.\n * @example\n * ```ts\n * import { parse } from 'graphql/language';\n * import { locatedError } from 'graphql/error';\n *\n * const document = parse('{ viewer { name } }');\n * const fieldNode = document.definitions[0].selectionSet.selections[0];\n * const error = locatedError(new Error('Resolver failed'), fieldNode, ['viewer']);\n *\n * error.message; // => 'Resolver failed'\n * error.locations; // => [{ line: 1, column: 3 }]\n * error.path; // => ['viewer']\n * ```\n */\nexport function locatedError(\n rawOriginalError: unknown,\n nodes: ASTNode | ReadonlyArray<ASTNode> | undefined | null,\n path?: Maybe<ReadonlyArray<string | number>>,\n): GraphQLError {\n const originalError = toError(rawOriginalError);\n\n // Note: this uses a brand-check to support GraphQL errors originating from other contexts.\n if (isLocatedGraphQLError(originalError)) {\n return originalError;\n }\n\n return new GraphQLError(originalError.message, {\n nodes: (originalError as GraphQLError).nodes ?? nodes,\n source: (originalError as GraphQLError).source,\n positions: (originalError as GraphQLError).positions,\n path,\n originalError,\n });\n}\n\nfunction isLocatedGraphQLError(error: any): error is GraphQLError {\n return Array.isArray(error.path);\n}\n"]}