UNPKG

5.05 kBJavaScriptView Raw
1import isObjectLike from '../jsutils/isObjectLike';
2import { getLocation } from '../language/location';
3import { printLocation, printSourceLocation } from '../language/printLocation';
4/**
5 * A GraphQLError describes an Error found during the parse, validate, or
6 * execute phases of performing a GraphQL operation. In addition to a message
7 * and stack trace, it also includes information about the locations in a
8 * GraphQL document and/or execution result that correspond to the Error.
9 */
10
11export function GraphQLError( // eslint-disable-line no-redeclare
12message, nodes, source, positions, path, originalError, extensions) {
13 // Compute list of blame nodes.
14 var _nodes = Array.isArray(nodes) ? nodes.length !== 0 ? nodes : undefined : nodes ? [nodes] : undefined; // Compute locations in the source for the given nodes/positions.
15
16
17 var _source = source;
18
19 if (!_source && _nodes) {
20 var node = _nodes[0];
21 _source = node && node.loc && node.loc.source;
22 }
23
24 var _positions = positions;
25
26 if (!_positions && _nodes) {
27 _positions = _nodes.reduce(function (list, node) {
28 if (node.loc) {
29 list.push(node.loc.start);
30 }
31
32 return list;
33 }, []);
34 }
35
36 if (_positions && _positions.length === 0) {
37 _positions = undefined;
38 }
39
40 var _locations;
41
42 if (positions && source) {
43 _locations = positions.map(function (pos) {
44 return getLocation(source, pos);
45 });
46 } else if (_nodes) {
47 _locations = _nodes.reduce(function (list, node) {
48 if (node.loc) {
49 list.push(getLocation(node.loc.source, node.loc.start));
50 }
51
52 return list;
53 }, []);
54 }
55
56 var _extensions = extensions;
57
58 if (_extensions == null && originalError != null) {
59 var originalExtensions = originalError.extensions;
60
61 if (isObjectLike(originalExtensions)) {
62 _extensions = originalExtensions;
63 }
64 }
65
66 Object.defineProperties(this, {
67 message: {
68 value: message,
69 // By being enumerable, JSON.stringify will include `message` in the
70 // resulting output. This ensures that the simplest possible GraphQL
71 // service adheres to the spec.
72 enumerable: true,
73 writable: true
74 },
75 locations: {
76 // Coercing falsey values to undefined ensures they will not be included
77 // in JSON.stringify() when not provided.
78 value: _locations || undefined,
79 // By being enumerable, JSON.stringify will include `locations` in the
80 // resulting output. This ensures that the simplest possible GraphQL
81 // service adheres to the spec.
82 enumerable: Boolean(_locations)
83 },
84 path: {
85 // Coercing falsey values to undefined ensures they will not be included
86 // in JSON.stringify() when not provided.
87 value: path || undefined,
88 // By being enumerable, JSON.stringify will include `path` in the
89 // resulting output. This ensures that the simplest possible GraphQL
90 // service adheres to the spec.
91 enumerable: Boolean(path)
92 },
93 nodes: {
94 value: _nodes || undefined
95 },
96 source: {
97 value: _source || undefined
98 },
99 positions: {
100 value: _positions || undefined
101 },
102 originalError: {
103 value: originalError
104 },
105 extensions: {
106 // Coercing falsey values to undefined ensures they will not be included
107 // in JSON.stringify() when not provided.
108 value: _extensions || undefined,
109 // By being enumerable, JSON.stringify will include `path` in the
110 // resulting output. This ensures that the simplest possible GraphQL
111 // service adheres to the spec.
112 enumerable: Boolean(_extensions)
113 }
114 }); // Include (non-enumerable) stack trace.
115
116 if (originalError && originalError.stack) {
117 Object.defineProperty(this, 'stack', {
118 value: originalError.stack,
119 writable: true,
120 configurable: true
121 });
122 } else if (Error.captureStackTrace) {
123 Error.captureStackTrace(this, GraphQLError);
124 } else {
125 Object.defineProperty(this, 'stack', {
126 value: Error().stack,
127 writable: true,
128 configurable: true
129 });
130 }
131}
132GraphQLError.prototype = Object.create(Error.prototype, {
133 constructor: {
134 value: GraphQLError
135 },
136 name: {
137 value: 'GraphQLError'
138 },
139 toString: {
140 value: function toString() {
141 return printError(this);
142 }
143 }
144});
145/**
146 * Prints a GraphQLError to a string, representing useful location information
147 * about the error's position in the source.
148 */
149
150export function printError(error) {
151 var output = error.message;
152
153 if (error.nodes) {
154 for (var _i2 = 0, _error$nodes2 = error.nodes; _i2 < _error$nodes2.length; _i2++) {
155 var node = _error$nodes2[_i2];
156
157 if (node.loc) {
158 output += '\n\n' + printLocation(node.loc);
159 }
160 }
161 } else if (error.source && error.locations) {
162 for (var _i4 = 0, _error$locations2 = error.locations; _i4 < _error$locations2.length; _i4++) {
163 var location = _error$locations2[_i4];
164 output += '\n\n' + printSourceLocation(error.source, location);
165 }
166 }
167
168 return output;
169}