UNPKG

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