1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function createUserError(message, locations, nodes) {
|
18 | var _nodes;
|
19 |
|
20 | var messageWithLocations = message;
|
21 |
|
22 | if (locations != null) {
|
23 | var printedLocations = printLocations(locations);
|
24 | messageWithLocations = printedLocations.length === 0 ? message : [message].concat(printedLocations).join('\n\n') + '\n';
|
25 | }
|
26 |
|
27 | return new (require("graphql").GraphQLError)(messageWithLocations, (_nodes = nodes) !== null && _nodes !== void 0 ? _nodes : []);
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function createNonRecoverableUserError(message, locations, nodes) {
|
37 | var _nodes2;
|
38 |
|
39 | var messageWithLocations = message;
|
40 |
|
41 | if (locations != null) {
|
42 | var printedLocations = printLocations(locations);
|
43 | messageWithLocations = printedLocations.length === 0 ? message : [message].concat(printedLocations).join('\n\n') + '\n';
|
44 | }
|
45 |
|
46 | var error = new (require("graphql").GraphQLError)(messageWithLocations, (_nodes2 = nodes) !== null && _nodes2 !== void 0 ? _nodes2 : []);
|
47 | return new Error(error.message);
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | function createCompilerError(message, locations, nodes) {
|
56 | var _nodes3;
|
57 |
|
58 | var messageWithLocations = message;
|
59 |
|
60 | if (locations != null) {
|
61 | var printedLocations = printLocations(locations);
|
62 | messageWithLocations = printedLocations.length === 0 ? message : [message].concat(printedLocations).join('\n\n') + '\n';
|
63 | }
|
64 |
|
65 | var error = new (require("graphql").GraphQLError)("Internal Error: ".concat(messageWithLocations), (_nodes3 = nodes) !== null && _nodes3 !== void 0 ? _nodes3 : []);
|
66 | return new Error(error.message);
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | function createCombinedError(errors, maybePrefix) {
|
75 | var prefix = maybePrefix != null ? "".concat(maybePrefix, ": ") : '';
|
76 | return new Error("".concat(prefix, "Encountered ").concat(errors.length, " error(s):\n") + errors.map(function (error) {
|
77 | return String(error).split('\n').map(function (line, index) {
|
78 | return index === 0 ? "- ".concat(line) : " ".concat(line);
|
79 | }).join('\n');
|
80 | }).join('\n'));
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function eachWithErrors(iterable, fn) {
|
94 | var errors = [];
|
95 | var _iteratorNormalCompletion = true;
|
96 | var _didIteratorError = false;
|
97 | var _iteratorError = undefined;
|
98 |
|
99 | try {
|
100 | for (var _iterator = iterable[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
101 | var item = _step.value;
|
102 |
|
103 | try {
|
104 | fn(item);
|
105 | } catch (error) {
|
106 | if (error instanceof require("graphql").GraphQLError) {
|
107 | errors.push(error);
|
108 | } else {
|
109 | throw error;
|
110 | }
|
111 | }
|
112 | }
|
113 | } catch (err) {
|
114 | _didIteratorError = true;
|
115 | _iteratorError = err;
|
116 | } finally {
|
117 | try {
|
118 | if (!_iteratorNormalCompletion && _iterator["return"] != null) {
|
119 | _iterator["return"]();
|
120 | }
|
121 | } finally {
|
122 | if (_didIteratorError) {
|
123 | throw _iteratorError;
|
124 | }
|
125 | }
|
126 | }
|
127 |
|
128 | if (errors.length !== 0) {
|
129 | return errors;
|
130 | }
|
131 |
|
132 | return null;
|
133 | }
|
134 |
|
135 | function printLocations(locations) {
|
136 | var printedLocations = [];
|
137 | var _iteratorNormalCompletion2 = true;
|
138 | var _didIteratorError2 = false;
|
139 | var _iteratorError2 = undefined;
|
140 |
|
141 | try {
|
142 | for (var _iterator2 = locations[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
|
143 | var location = _step2.value;
|
144 | var sourceLocation = location;
|
145 |
|
146 | while (sourceLocation.kind === 'Derived') {
|
147 | sourceLocation = sourceLocation.source;
|
148 | }
|
149 |
|
150 | switch (sourceLocation.kind) {
|
151 | case 'Source':
|
152 | {
|
153 |
|
154 | var prefix = sourceLocation === location ? 'Source: ' : 'Source (derived): ';
|
155 | printedLocations.push(prefix + highlightSourceAtLocation(sourceLocation.source, getLocation(sourceLocation.source, sourceLocation.start)));
|
156 | break;
|
157 | }
|
158 |
|
159 | case 'Generated':
|
160 | {
|
161 | printedLocations.push('Source: (generated)');
|
162 | break;
|
163 | }
|
164 |
|
165 | case 'Unknown':
|
166 | {
|
167 | printedLocations.push('Source: (unknown)');
|
168 | break;
|
169 | }
|
170 |
|
171 | default:
|
172 | {
|
173 | sourceLocation;
|
174 | throw createCompilerError("RelayCompilerError: cannot print location '".concat(String(sourceLocation), "'."));
|
175 | }
|
176 | }
|
177 | }
|
178 | } catch (err) {
|
179 | _didIteratorError2 = true;
|
180 | _iteratorError2 = err;
|
181 | } finally {
|
182 | try {
|
183 | if (!_iteratorNormalCompletion2 && _iterator2["return"] != null) {
|
184 | _iterator2["return"]();
|
185 | }
|
186 | } finally {
|
187 | if (_didIteratorError2) {
|
188 | throw _iteratorError2;
|
189 | }
|
190 | }
|
191 | }
|
192 |
|
193 | return printedLocations;
|
194 | }
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | function highlightSourceAtLocation(source, location) {
|
202 | var firstLineColumnOffset = source.locationOffset.column - 1;
|
203 | var body = whitespace(firstLineColumnOffset) + source.body;
|
204 | var lineIndex = location.line - 1;
|
205 | var lineOffset = source.locationOffset.line - 1;
|
206 | var lineNum = location.line + lineOffset;
|
207 | var columnOffset = location.line === 1 ? firstLineColumnOffset : 0;
|
208 | var columnNum = location.column + columnOffset;
|
209 | var lines = body.split(/\r\n|[\n\r]/g);
|
210 | return "".concat(source.name, " (").concat(lineNum, ":").concat(columnNum, ")\n") + printPrefixedLines([
|
211 | ["".concat(lineNum - 1, ": "), lines[lineIndex - 1]], ["".concat(lineNum, ": "), lines[lineIndex]], ['', whitespace(columnNum - 1) + '^'], ["".concat(lineNum + 1, ": "), lines[lineIndex + 1]]]);
|
212 | }
|
213 |
|
214 | function printPrefixedLines(lines) {
|
215 | var existingLines = lines.filter(function (_ref) {
|
216 | var _ = _ref[0],
|
217 | line = _ref[1];
|
218 | return line !== undefined;
|
219 | });
|
220 | var padLen = 0;
|
221 | var _iteratorNormalCompletion3 = true;
|
222 | var _didIteratorError3 = false;
|
223 | var _iteratorError3 = undefined;
|
224 |
|
225 | try {
|
226 | for (var _iterator3 = existingLines[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
|
227 | var _step3$value = _step3.value,
|
228 | prefix = _step3$value[0];
|
229 | padLen = Math.max(padLen, prefix.length);
|
230 | }
|
231 | } catch (err) {
|
232 | _didIteratorError3 = true;
|
233 | _iteratorError3 = err;
|
234 | } finally {
|
235 | try {
|
236 | if (!_iteratorNormalCompletion3 && _iterator3["return"] != null) {
|
237 | _iterator3["return"]();
|
238 | }
|
239 | } finally {
|
240 | if (_didIteratorError3) {
|
241 | throw _iteratorError3;
|
242 | }
|
243 | }
|
244 | }
|
245 |
|
246 | return existingLines.map(function (_ref2) {
|
247 | var prefix = _ref2[0],
|
248 | line = _ref2[1];
|
249 | return lpad(padLen, prefix) + line;
|
250 | }).join('\n');
|
251 | }
|
252 |
|
253 | function whitespace(len) {
|
254 | return Array(len + 1).join(' ');
|
255 | }
|
256 |
|
257 | function lpad(len, str) {
|
258 | return whitespace(len - str.length) + str;
|
259 | }
|
260 |
|
261 | function getLocation(source, position) {
|
262 | var lineRegexp = /\r\n|[\n\r]/g;
|
263 | var line = 1;
|
264 | var column = position + 1;
|
265 | var match;
|
266 |
|
267 | while ((match = lineRegexp.exec(source.body)) && match.index < position) {
|
268 | line += 1;
|
269 | column = position + 1 - (match.index + match[0].length);
|
270 | }
|
271 |
|
272 | return {
|
273 | line: line,
|
274 | column: column
|
275 | };
|
276 | }
|
277 |
|
278 | module.exports = {
|
279 | createCombinedError: createCombinedError,
|
280 | createCompilerError: createCompilerError,
|
281 | createNonRecoverableUserError: createNonRecoverableUserError,
|
282 | createUserError: createUserError,
|
283 | eachWithErrors: eachWithErrors
|
284 | }; |
\ | No newline at end of file |