|
10 | 10 |
|
11 | 11 | import { getLocation } from '../language';
|
12 | 12 | import type { Node } from '../language/ast';
|
| 13 | +import type { Source } from '../language/source'; |
13 | 14 |
|
14 | 15 |
|
15 | 16 | export class GraphQLError extends Error {
|
16 | 17 | message: string;
|
17 | 18 | stack: string;
|
18 | 19 | nodes: ?Array<Node>;
|
19 |
| - source: any; |
20 |
| - positions: any; |
| 20 | + source: Source; |
| 21 | + positions: Array<number>; |
21 | 22 | locations: any;
|
22 | 23 |
|
23 | 24 | constructor(
|
24 | 25 | message: string,
|
25 | 26 | // A flow bug keeps us from declaring nodes as an array of Node
|
26 | 27 | nodes?: Array<any/* Node */>,
|
27 |
| - stack?: ?string |
| 28 | + stack?: ?string, |
| 29 | + source?: Source, |
| 30 | + positions?: Array<number> |
28 | 31 | ) {
|
29 | 32 | super(message);
|
30 | 33 | this.message = message;
|
| 34 | + |
31 | 35 | Object.defineProperty(this, 'stack', { value: stack || message });
|
32 | 36 | Object.defineProperty(this, 'nodes', { value: nodes });
|
33 |
| - } |
34 |
| -} |
35 | 37 |
|
36 |
| -// Note: flow does not yet know about Object.defineProperty with `get`. |
37 |
| -Object.defineProperty(GraphQLError.prototype, 'source', ({ |
38 |
| - get() { |
39 |
| - var nodes = this.nodes; |
40 |
| - if (nodes && nodes.length > 0) { |
41 |
| - var node = nodes[0]; |
42 |
| - return node && node.loc && node.loc.source; |
43 |
| - } |
44 |
| - } |
45 |
| -}: any)); |
| 38 | + // Note: flow does not yet know about Object.defineProperty with `get`. |
| 39 | + Object.defineProperty(this, 'source', ({ |
| 40 | + get() { |
| 41 | + if (source) { |
| 42 | + return source; |
| 43 | + } |
| 44 | + if (nodes && nodes.length > 0) { |
| 45 | + var node = nodes[0]; |
| 46 | + return node && node.loc && node.loc.source; |
| 47 | + } |
| 48 | + } |
| 49 | + }: any)); |
46 | 50 |
|
47 |
| -Object.defineProperty(GraphQLError.prototype, 'positions', ({ |
48 |
| - get() { |
49 |
| - var nodes = this.nodes; |
50 |
| - if (nodes) { |
51 |
| - var positions = nodes.map(node => node.loc && node.loc.start); |
52 |
| - if (positions.some(p => p)) { |
53 |
| - return positions; |
| 51 | + Object.defineProperty(this, 'positions', ({ |
| 52 | + get() { |
| 53 | + if (positions) { |
| 54 | + return positions; |
| 55 | + } |
| 56 | + if (nodes) { |
| 57 | + var nodePositions = nodes.map(node => node.loc && node.loc.start); |
| 58 | + if (nodePositions.some(p => p)) { |
| 59 | + return nodePositions; |
| 60 | + } |
| 61 | + } |
54 | 62 | }
|
55 |
| - } |
56 |
| - } |
57 |
| -}: any)); |
| 63 | + }: any)); |
58 | 64 |
|
59 |
| -Object.defineProperty(GraphQLError.prototype, 'locations', ({ |
60 |
| - get() { |
61 |
| - var positions = this.positions; |
62 |
| - var source = this.source; |
63 |
| - if (positions && source) { |
64 |
| - return positions.map(pos => getLocation(source, pos)); |
65 |
| - } |
| 65 | + Object.defineProperty(this, 'locations', ({ |
| 66 | + get() { |
| 67 | + if (this.positions && this.source) { |
| 68 | + return this.positions.map(pos => getLocation(this.source, pos)); |
| 69 | + } |
| 70 | + } |
| 71 | + }: any)); |
66 | 72 | }
|
67 |
| -}: any)); |
| 73 | +} |
0 commit comments