-
-
Notifications
You must be signed in to change notification settings - Fork 562
/
Error.php
289 lines (254 loc) · 8.37 KB
/
Error.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php declare(strict_types=1);
namespace GraphQL\Error;
use GraphQL\Language\AST\Node;
use GraphQL\Language\Source;
use GraphQL\Language\SourceLocation;
/**
* Describes an Error found during the parse, validate, or
* execute phases of performing a GraphQL operation. In addition to a message
* and stack trace, it also includes information about the locations in a
* GraphQL document and/or execution result that correspond to the Error.
*
* When the error was caused by an exception thrown in resolver, original exception
* is available via `getPrevious()`.
*
* Also read related docs on [error handling](error-handling.md)
*
* Class extends standard PHP `\Exception`, so all standard methods of base `\Exception` class
* are available in addition to those listed below.
*
* @see \GraphQL\Tests\Error\ErrorTest
*/
class Error extends \Exception implements \JsonSerializable, ClientAware, ProvidesExtensions
{
/**
* Lazily initialized.
*
* @var array<int, SourceLocation>
*/
private array $locations;
/**
* An array describing the JSON-path into the execution response which
* corresponds to this error. Only included for errors during execution.
*
* @var array<int, int|string>|null
*/
public ?array $path;
/**
* An array of GraphQL AST Nodes corresponding to this error.
*
* @var array<Node>|null
*/
public ?array $nodes;
/**
* The source GraphQL document for the first location of this error.
*
* Note that if this Error represents more than one node, the source may not
* represent nodes after the first node.
*/
private ?Source $source;
/** @var array<int, int>|null */
private ?array $positions;
private bool $isClientSafe;
/** @var array<string, mixed>|null */
protected ?array $extensions;
/**
* @param iterable<array-key, Node|null>|Node|null $nodes
* @param array<int, int>|null $positions
* @param array<int, int|string>|null $path
* @param array<string, mixed>|null $extensions
*/
public function __construct(
string $message = '',
$nodes = null,
?Source $source = null,
?array $positions = null,
?array $path = null,
?\Throwable $previous = null,
?array $extensions = null
) {
parent::__construct($message, 0, $previous);
// Compute list of blame nodes.
if ($nodes instanceof \Traversable) {
$this->nodes = array_filter(\iterator_to_array($nodes));
} elseif (\is_array($nodes)) {
$this->nodes = array_filter($nodes);
} elseif ($nodes !== null) {
$this->nodes = [$nodes];
} else {
$this->nodes = null;
}
$this->source = $source;
$this->positions = $positions;
$this->path = $path;
if (\is_array($extensions) && $extensions !== []) {
$this->extensions = $extensions;
} elseif ($previous instanceof ProvidesExtensions) {
$this->extensions = $previous->getExtensions();
} else {
$this->extensions = null;
}
$this->isClientSafe = $previous instanceof ClientAware
? $previous->isClientSafe()
: $previous === null;
}
/**
* Given an arbitrary Error, presumably thrown while attempting to execute a
* GraphQL operation, produce a new GraphQLError aware of the location in the
* document responsible for the original Error.
*
* @param mixed $error
* @param iterable<Node>|Node|null $nodes
* @param array<int, int|string>|null $path
*/
public static function createLocatedError($error, $nodes = null, ?array $path = null): Error
{
if ($error instanceof self) {
if ($error->isLocated()) {
return $error;
}
$nodes ??= $error->getNodes();
$path ??= $error->getPath();
}
$source = null;
$originalError = null;
$positions = [];
$extensions = [];
if ($error instanceof self) {
$message = $error->getMessage();
$originalError = $error;
$source = $error->getSource();
$positions = $error->getPositions();
$extensions = $error->getExtensions();
} elseif ($error instanceof InvariantViolation) {
$message = $error->getMessage();
$originalError = $error->getPrevious() ?? $error;
} elseif ($error instanceof \Throwable) {
$message = $error->getMessage();
$originalError = $error;
} else {
$message = (string) $error;
}
$nonEmptyMessage = $message === ''
? 'An unknown error occurred.'
: $message;
return new static(
$nonEmptyMessage,
$nodes,
$source,
$positions,
$path,
$originalError,
$extensions
);
}
protected function isLocated(): bool
{
$path = $this->getPath();
$nodes = $this->getNodes();
return $path !== null
&& $path !== []
&& $nodes !== null
&& $nodes !== [];
}
public function isClientSafe(): bool
{
return $this->isClientSafe;
}
public function getSource(): ?Source
{
return $this->source
??= $this->nodes[0]->loc->source
?? null;
}
/** @return array<int, int> */
public function getPositions(): array
{
if (! isset($this->positions)) {
$this->positions = [];
if (isset($this->nodes)) {
foreach ($this->nodes as $node) {
if (isset($node->loc->start)) {
$this->positions[] = $node->loc->start;
}
}
}
}
return $this->positions;
}
/**
* An array of locations within the source GraphQL document which correspond to this error.
*
* Each entry has information about `line` and `column` within source GraphQL document:
* $location->line;
* $location->column;
*
* Errors during validation often contain multiple locations, for example to
* point out to field mentioned in multiple fragments. Errors during execution include a
* single location, the field which produced the error.
*
* @return array<int, SourceLocation>
*
* @api
*/
public function getLocations(): array
{
if (! isset($this->locations)) {
$positions = $this->getPositions();
$source = $this->getSource();
$nodes = $this->getNodes();
$this->locations = [];
if ($source !== null && $positions !== []) {
foreach ($positions as $position) {
$this->locations[] = $source->getLocation($position);
}
} elseif ($nodes !== null && $nodes !== []) {
foreach ($nodes as $node) {
if (isset($node->loc->source)) {
$this->locations[] = $node->loc->source->getLocation($node->loc->start);
}
}
}
}
return $this->locations;
}
/** @return array<Node>|null */
public function getNodes(): ?array
{
return $this->nodes;
}
/**
* Returns an array describing the path from the root value to the field which produced this error.
* Only included for execution errors.
*
* @return array<int, int|string>|null
*
* @api
*/
public function getPath(): ?array
{
return $this->path;
}
/** @return array<string, mixed>|null */
public function getExtensions(): ?array
{
return $this->extensions;
}
/**
* Specify data which should be serialized to JSON.
*
* @see http://php.net/manual/en/jsonserializable.jsonserialize.php
*
* @return array<string, mixed> data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource
*/
#[\ReturnTypeWillChange]
public function jsonSerialize(): array
{
return FormattedError::createFromException($this);
}
public function __toString(): string
{
return FormattedError::printError($this);
}
}