1 | import isObjectLike from '../jsutils/isObjectLike';
|
2 | import { getLocation } from '../language/location';
|
3 | import { printLocation, printSourceLocation } from '../language/printLocation';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export function GraphQLError( // eslint-disable-line no-redeclare
|
12 | message, nodes, source, positions, path, originalError, extensions) {
|
13 |
|
14 | var _nodes = Array.isArray(nodes) ? nodes.length !== 0 ? nodes : undefined : nodes ? [nodes] : undefined;
|
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 |
|
70 |
|
71 |
|
72 | enumerable: true,
|
73 | writable: true
|
74 | },
|
75 | locations: {
|
76 |
|
77 |
|
78 | value: _locations || undefined,
|
79 |
|
80 |
|
81 |
|
82 | enumerable: Boolean(_locations)
|
83 | },
|
84 | path: {
|
85 |
|
86 |
|
87 | value: path || undefined,
|
88 |
|
89 |
|
90 |
|
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 |
|
107 |
|
108 | value: _extensions || undefined,
|
109 |
|
110 |
|
111 |
|
112 | enumerable: Boolean(_extensions)
|
113 | }
|
114 | });
|
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 | }
|
132 | GraphQLError.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 |
|
147 |
|
148 |
|
149 |
|
150 | export 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 | }
|