UNPKG

8.56 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * strict
8 * @format
9 */
10'use strict';
11
12/**
13 * Creates an error describing invalid application code (GraphQL/Schema)
14 * that must be fixed by the end developer. This should only be used
15 * for local errors that don't affect processing of other user code.
16 */
17function 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 * Similar to createUserError but for errors that are *not* recoverable:
31 * the compiler should not continue to process other inputs because their
32 * validity can't be determined.
33 */
34
35
36function 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 * Creates an error describing a problem with the compiler itself - such
51 * as a broken invariant - that must be fixed within the compiler.
52 */
53
54
55function 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 * Merges the results of multiple user errors into one so that they
70 * can be reported in bulk.
71 */
72
73
74function 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 * Iterates over the elements of some iterable value, calling the
84 * supplied callback for each item with a guard for user errors.
85 * Returns null if the iteration completed without errors, otherwise
86 * returns an array of all the user errors encountered.
87 *
88 * Note that non-user errors are rethrown since they are
89 * non-recoverable.
90 */
91
92
93function 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
135function 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 // source location
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 * Render a helpful description of the location of the error in the GraphQL
197 * Source document.
198 */
199
200
201function 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([// Lines specified like this: ["prefix", "string"],
211 ["".concat(lineNum - 1, ": "), lines[lineIndex - 1]], ["".concat(lineNum, ": "), lines[lineIndex]], ['', whitespace(columnNum - 1) + '^'], ["".concat(lineNum + 1, ": "), lines[lineIndex + 1]]]);
212}
213
214function 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
253function whitespace(len) {
254 return Array(len + 1).join(' ');
255}
256
257function lpad(len, str) {
258 return whitespace(len - str.length) + str;
259}
260
261function 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
278module.exports = {
279 createCombinedError: createCombinedError,
280 createCompilerError: createCompilerError,
281 createNonRecoverableUserError: createNonRecoverableUserError,
282 createUserError: createUserError,
283 eachWithErrors: eachWithErrors
284};
\No newline at end of file