1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.GraphQLError = GraphQLError;
|
7 | exports.printError = printError;
|
8 |
|
9 | var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike"));
|
10 |
|
11 | var _location = require("../language/location");
|
12 |
|
13 | var _printLocation = require("../language/printLocation");
|
14 |
|
15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
16 |
|
17 | function GraphQLError( // eslint-disable-line no-redeclare
|
18 | message, nodes, source, positions, path, originalError, extensions) {
|
19 |
|
20 | var _nodes = Array.isArray(nodes) ? nodes.length !== 0 ? nodes : undefined : nodes ? [nodes] : undefined;
|
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 |
|
76 |
|
77 |
|
78 | enumerable: true,
|
79 | writable: true
|
80 | },
|
81 | locations: {
|
82 |
|
83 |
|
84 | value: _locations || undefined,
|
85 |
|
86 |
|
87 |
|
88 | enumerable: Boolean(_locations)
|
89 | },
|
90 | path: {
|
91 |
|
92 |
|
93 | value: path || undefined,
|
94 |
|
95 |
|
96 |
|
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 |
|
113 |
|
114 | value: _extensions || undefined,
|
115 |
|
116 |
|
117 |
|
118 | enumerable: Boolean(_extensions)
|
119 | }
|
120 | });
|
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 |
|
139 | GraphQLError.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 |
|
154 |
|
155 |
|
156 |
|
157 | function 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 | }
|