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