1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Error; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Language\Node\NodeInterface; |
6
|
|
|
use Digia\GraphQL\Language\Location; |
7
|
|
|
use Digia\GraphQL\Language\Source; |
8
|
|
|
use Digia\GraphQL\Language\SourceLocation; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* An GraphQLException describes an exception thrown during the execute |
12
|
|
|
* phase of performing a GraphQL operation. In addition to a message |
13
|
|
|
* and stack trace, it also includes information about the locations in a |
14
|
|
|
* GraphQL document and/or execution result that correspond to the Error. |
15
|
|
|
*/ |
16
|
|
|
class GraphQLException extends AbstractException |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* An array of { line, column } locations within the source GraphQL document |
21
|
|
|
* which correspond to this error. |
22
|
|
|
* |
23
|
|
|
* Errors during validation often contain multiple locations, for example to |
24
|
|
|
* point out two things with the same name. Errors during execution include a |
25
|
|
|
* single location, the field which produced the error. |
26
|
|
|
* |
27
|
|
|
* @var array|null |
28
|
|
|
*/ |
29
|
|
|
protected $locations; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* An array describing the JSON-path into the execution response which |
33
|
|
|
* corresponds to this error. Only included for errors during execution. |
34
|
|
|
* |
35
|
|
|
* @var string[]|null |
36
|
|
|
*/ |
37
|
|
|
protected $path; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* An array of GraphQL AST Nodes corresponding to this error. |
41
|
|
|
* |
42
|
|
|
* @var NodeInterface[]|null |
43
|
|
|
*/ |
44
|
|
|
protected $nodes; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The source GraphQL document for the first location of this error. |
48
|
|
|
* |
49
|
|
|
* Note that if this Error represents more than one node, the source may not |
50
|
|
|
* represent nodes after the first node. |
51
|
|
|
* |
52
|
|
|
* @var Source|null |
53
|
|
|
*/ |
54
|
|
|
protected $source; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* An array of character offsets within the source GraphQL document |
58
|
|
|
* which correspond to this error. |
59
|
|
|
* |
60
|
|
|
* @var int[]|null |
61
|
|
|
*/ |
62
|
|
|
protected $positions; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Extension fields to add to the formatted error. |
66
|
|
|
* |
67
|
|
|
* @var array|null |
68
|
|
|
*/ |
69
|
|
|
protected $extensions; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* ExecutionException constructor. |
73
|
|
|
* |
74
|
|
|
* @param string $message |
75
|
|
|
* @param array|null $nodes |
76
|
|
|
* @param Source|null $source |
77
|
|
|
* @param array|null $positions |
78
|
|
|
* @param array|null $path |
79
|
|
|
* @param array|null $extensions |
80
|
|
|
*/ |
81
|
|
|
public function __construct( |
82
|
|
|
string $message, |
83
|
|
|
?array $nodes = null, |
84
|
|
|
?Source $source = null, |
85
|
|
|
?array $positions = null, |
86
|
|
|
?array $path = null, |
87
|
|
|
?array $extensions = null |
88
|
|
|
) { |
89
|
|
|
parent::__construct($message); |
90
|
|
|
|
91
|
|
|
$this->resolveNodes($nodes); |
92
|
|
|
$this->resolveSource($source); |
93
|
|
|
$this->resolvePositions($positions); |
94
|
|
|
$this->resolveLocations($positions, $source); |
95
|
|
|
|
96
|
|
|
$this->path = $path; |
97
|
|
|
$this->extensions = $extensions; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return NodeInterface[] |
102
|
|
|
*/ |
103
|
|
|
public function getNodes(): ?array |
104
|
|
|
{ |
105
|
|
|
return $this->nodes; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function hasSource(): bool |
112
|
|
|
{ |
113
|
|
|
return null !== $this->source; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return Source|null |
118
|
|
|
*/ |
119
|
|
|
public function getSource(): ?Source |
120
|
|
|
{ |
121
|
|
|
return $this->source; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return int[]|null |
126
|
|
|
*/ |
127
|
|
|
public function getPositions(): ?array |
128
|
|
|
{ |
129
|
|
|
return $this->positions; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
public function hasLocations(): bool |
136
|
|
|
{ |
137
|
|
|
return !empty($this->locations); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return array|null |
142
|
|
|
*/ |
143
|
|
|
public function getLocations(): ?array |
144
|
|
|
{ |
145
|
|
|
return $this->locations; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return array|null |
150
|
|
|
*/ |
151
|
|
|
public function getLocationsAsArray(): ?array |
152
|
|
|
{ |
153
|
|
|
return !empty($this->locations) ? array_map(function (SourceLocation $location) { |
154
|
|
|
return $location->toArray(); |
155
|
|
|
}, $this->locations) : null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return array|null |
160
|
|
|
*/ |
161
|
|
|
public function getPath(): ?array |
162
|
|
|
{ |
163
|
|
|
return $this->path; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return array|null |
168
|
|
|
*/ |
169
|
|
|
public function getExtensions(): ?array |
170
|
|
|
{ |
171
|
|
|
return $this->extensions; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param array|null $nodes |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
|
|
protected function resolveNodes(?array $nodes) |
179
|
|
|
{ |
180
|
|
|
$this->nodes = is_array($nodes) |
181
|
|
|
? (!empty($nodes) ? $nodes : null) |
182
|
|
|
: (null !== $nodes ? [$nodes] : null); |
|
|
|
|
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param Source|null $source |
189
|
|
|
* @return $this |
190
|
|
|
*/ |
191
|
|
|
protected function resolveSource(?Source $source) |
192
|
|
|
{ |
193
|
|
|
if (null === $source && !empty($this->nodes)) { |
194
|
|
|
$firstNode = $this->nodes[0]; |
195
|
|
|
$location = null !== $firstNode ? $firstNode->getLocation() : null; |
196
|
|
|
$source = null !== $location ? $location->getSource() : null; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$this->source = $source; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param array|null $positions |
206
|
|
|
* @return $this |
207
|
|
|
*/ |
208
|
|
|
protected function resolvePositions(?array $positions) |
209
|
|
|
{ |
210
|
|
|
if (null === $positions && !empty($this->nodes)) { |
211
|
|
|
$positions = array_reduce($this->nodes, function (array $list, NodeInterface $node) { |
212
|
|
|
$location = $node->getLocation(); |
213
|
|
|
if (null !== $location) { |
214
|
|
|
$list[] = $location->getStart(); |
215
|
|
|
} |
216
|
|
|
return $list; |
217
|
|
|
}, []); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if (null !== $positions && empty($positions)) { |
221
|
|
|
$positions = null; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$this->positions = $positions; |
225
|
|
|
|
226
|
|
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param array|null $positions |
231
|
|
|
* @param Source|null $source |
232
|
|
|
* @return $this |
233
|
|
|
*/ |
234
|
|
|
protected function resolveLocations(?array $positions, ?Source $source) |
235
|
|
|
{ |
236
|
|
|
if (null !== $positions && null !== $source) { |
237
|
|
|
$locations = array_map(function ($position) use ($source) { |
238
|
|
|
return SourceLocation::fromSource($source, $position); |
239
|
|
|
}, $positions); |
240
|
|
|
} elseif (!empty($this->nodes)) { |
241
|
|
|
$locations = array_reduce($this->nodes, function (array $list, NodeInterface $node) { |
242
|
|
|
$location = $node->getLocation(); |
243
|
|
|
if (null !== $location) { |
244
|
|
|
$list[] = SourceLocation::fromSource($location->getSource(), $location->getStart()); |
245
|
|
|
} |
246
|
|
|
return $list; |
247
|
|
|
}, []); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if (isset($locations)) { |
251
|
|
|
$this->locations = $locations; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @inheritdoc |
259
|
|
|
*/ |
260
|
|
|
public function __toString(): string |
261
|
|
|
{ |
262
|
|
|
return printError($this); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|