Passed
Pull Request — master (#288)
by Christoffer
03:31
created

GraphQLException::setExtensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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