Mirror: The small sibling of the graphql package, slimmed down for client-side libraries.
1import { getLocation } from 'graphql/language/location';
2import { printLocation, printSourceLocation } from 'graphql/language/printLocation';
3
4export class GraphQLError extends Error {
5 constructor(
6 message,
7 nodes,
8 source,
9 positions,
10 path,
11 originalError,
12 extensions,
13 ) {
14 super(message);
15
16 this.name = 'GraphQLError';
17 this.message = message;
18
19 if (path) this.path = path;
20 if (nodes) this.nodes = nodes;
21 if (source) this.source = source;
22 if (positions) this.positions = positions;
23 if (originalError) this.originalError = originalError;
24
25 let _extensions = extensions;
26 if (_extensions == null && originalError != null) {
27 const originalExtensions = originalError.extensions;
28 if (isObjectLike(originalExtensions)) {
29 _extensions = originalExtensions;
30 }
31 }
32
33 if (_extensions) {
34 this.extensions = _extensions;
35 }
36 }
37
38 toString() {
39 return printError(this);
40 }
41}
42
43/**
44 * Prints a GraphQLError to a string, representing useful location information
45 * about the error's position in the source.
46 */
47export function printError(error) {
48 let output = error.message;
49
50 if (error.nodes) {
51 for (const node of error.nodes) {
52 if (node.loc) {
53 output += '\n\n' + printLocation(node.loc);
54 }
55 }
56 } else if (error.source && error.locations) {
57 for (const location of error.locations) {
58 output += '\n\n' + printSourceLocation(error.source, location);
59 }
60 }
61
62 return output;
63}