UNPKG

4.03 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.GraphQLError = GraphQLError;
7
8var _printError = require('./printError');
9
10var _location = require('../language/location');
11
12/**
13 * A GraphQLError describes an Error found during the parse, validate, or
14 * execute phases of performing a GraphQL operation. In addition to a message
15 * and stack trace, it also includes information about the locations in a
16 * GraphQL document and/or execution result that correspond to the Error.
17 */
18/**
19 * Copyright (c) 2015-present, Facebook, Inc.
20 *
21 * This source code is licensed under the MIT license found in the
22 * LICENSE file in the root directory of this source tree.
23 *
24 *
25 */
26
27function GraphQLError( // eslint-disable-line no-redeclare
28message, nodes, source, positions, path, originalError, extensions) {
29 // Compute list of blame nodes.
30 var _nodes = Array.isArray(nodes) ? nodes.length !== 0 ? nodes : undefined : nodes ? [nodes] : undefined;
31
32 // Compute locations in the source for the given nodes/positions.
33 var _source = source;
34 if (!_source && _nodes) {
35 var node = _nodes[0];
36 _source = node && node.loc && node.loc.source;
37 }
38
39 var _positions = positions;
40 if (!_positions && _nodes) {
41 _positions = _nodes.reduce(function (list, node) {
42 if (node.loc) {
43 list.push(node.loc.start);
44 }
45 return list;
46 }, []);
47 }
48 if (_positions && _positions.length === 0) {
49 _positions = undefined;
50 }
51
52 var _locations = void 0;
53 if (positions && source) {
54 var providedSource = source;
55 _locations = positions.map(function (pos) {
56 return (0, _location.getLocation)(providedSource, pos);
57 });
58 } else if (_nodes) {
59 _locations = _nodes.reduce(function (list, node) {
60 if (node.loc) {
61 list.push((0, _location.getLocation)(node.loc.source, node.loc.start));
62 }
63 return list;
64 }, []);
65 }
66
67 Object.defineProperties(this, {
68 message: {
69 value: message,
70 // By being enumerable, JSON.stringify will include `message` in the
71 // resulting output. This ensures that the simplest possible GraphQL
72 // service adheres to the spec.
73 enumerable: true,
74 writable: true
75 },
76 locations: {
77 // Coercing falsey values to undefined ensures they will not be included
78 // in JSON.stringify() when not provided.
79 value: _locations || undefined,
80 // By being enumerable, JSON.stringify will include `locations` in the
81 // resulting output. This ensures that the simplest possible GraphQL
82 // service adheres to the spec.
83 enumerable: true
84 },
85 path: {
86 // Coercing falsey values to undefined ensures they will not be included
87 // in JSON.stringify() when not provided.
88 value: path || undefined,
89 // By being enumerable, JSON.stringify will include `path` in the
90 // resulting output. This ensures that the simplest possible GraphQL
91 // service adheres to the spec.
92 enumerable: true
93 },
94 nodes: {
95 value: _nodes || undefined
96 },
97 source: {
98 value: _source || undefined
99 },
100 positions: {
101 value: _positions || undefined
102 },
103 originalError: {
104 value: originalError
105 },
106 extensions: {
107 value: extensions || originalError && originalError.extensions
108 }
109 });
110
111 // Include (non-enumerable) stack trace.
112 if (originalError && originalError.stack) {
113 Object.defineProperty(this, 'stack', {
114 value: originalError.stack,
115 writable: true,
116 configurable: true
117 });
118 } else if (Error.captureStackTrace) {
119 Error.captureStackTrace(this, GraphQLError);
120 } else {
121 Object.defineProperty(this, 'stack', {
122 value: Error().stack,
123 writable: true,
124 configurable: true
125 });
126 }
127}
128
129GraphQLError.prototype = Object.create(Error.prototype, {
130 constructor: { value: GraphQLError },
131 name: { value: 'GraphQLError' },
132 toString: {
133 value: function toString() {
134 return (0, _printError.printError)(this);
135 }
136 }
137});
\No newline at end of file