UNPKG

2.85 kBTypeScriptView Raw
1import Maybe from '../tsutils/Maybe';
2
3import { ASTNode } from '../language/ast';
4import { Source } from '../language/source';
5import { SourceLocation, getLocation } from '../language/location';
6
7/**
8 * A GraphQLError describes an Error found during the parse, validate, or
9 * execute phases of performing a GraphQL operation. In addition to a message
10 * and stack trace, it also includes information about the locations in a
11 * GraphQL document and/or execution result that correspond to the Error.
12 */
13export class GraphQLError extends Error {
14 constructor(
15 message: string,
16 nodes?: ReadonlyArray<ASTNode> | ASTNode | undefined,
17 source?: Maybe<Source>,
18 positions?: Maybe<ReadonlyArray<number>>,
19 path?: Maybe<ReadonlyArray<string | number>>,
20 originalError?: Maybe<Error>,
21 extensions?: Maybe<{ [key: string]: any }>,
22 );
23
24 /**
25 * A message describing the Error for debugging purposes.
26 *
27 * Enumerable, and appears in the result of JSON.stringify().
28 *
29 * Note: should be treated as readonly, despite invariant usage.
30 */
31 message: string;
32
33 /**
34 * An array of { line, column } locations within the source GraphQL document
35 * which correspond to this error.
36 *
37 * Errors during validation often contain multiple locations, for example to
38 * point out two things with the same name. Errors during execution include a
39 * single location, the field which produced the error.
40 *
41 * Enumerable, and appears in the result of JSON.stringify().
42 */
43 readonly locations: ReadonlyArray<SourceLocation> | undefined;
44
45 /**
46 * An array describing the JSON-path into the execution response which
47 * corresponds to this error. Only included for errors during execution.
48 *
49 * Enumerable, and appears in the result of JSON.stringify().
50 */
51 readonly path: ReadonlyArray<string | number> | undefined;
52
53 /**
54 * An array of GraphQL AST Nodes corresponding to this error.
55 */
56 readonly nodes: ReadonlyArray<ASTNode> | undefined;
57
58 /**
59 * The source GraphQL document corresponding to this error.
60 *
61 * Note that if this Error represents more than one node, the source may not
62 * represent nodes after the first node.
63 */
64 readonly source: Source | undefined;
65
66 /**
67 * An array of character offsets within the source GraphQL document
68 * which correspond to this error.
69 */
70 readonly positions: ReadonlyArray<number> | undefined;
71
72 /**
73 * The original error thrown from a field resolver during execution.
74 */
75 readonly originalError: Maybe<Error>;
76
77 /**
78 * Extension fields to add to the formatted error.
79 */
80 readonly extensions: { [key: string]: any } | undefined;
81}
82
83/**
84 * Prints a GraphQLError to a string, representing useful location information
85 * about the error's position in the source.
86 */
87export function printError(error: GraphQLError): string;