Completed
Pull Request — master (#118)
by Christoffer
02:25
created

GraphQLException::getLocations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\JsonEncodeArrayTrait;
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 JsonEncodeArrayTrait;
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 \Exception|null
70
     */
71
    protected $originalException;
72
73
    /**
74
     * ExecutionException constructor.
75
     *
76
     * @param string          $message
77
     * @param array|null      $nodes
78
     * @param Source|null     $source
79
     * @param array|null      $positions
80
     * @param array|null      $path
81
     * @param \Exception|null $originalException
82
     */
83
    public function __construct(
84
        string $message,
85
        ?array $nodes = null,
86
        ?Source $source = null,
87
        ?array $positions = null,
88
        ?array $path = null,
89
        ?\Exception $originalException = null
90
    ) {
91
        parent::__construct($message);
92
93
        $this->resolveNodes($nodes);
94
        $this->resolveSource($source);
95
        $this->resolvePositions($positions);
96
        $this->resolveLocations($positions, $source);
97
98
        $this->path              = $path;
99
        $this->originalException = $originalException;
100
    }
101
102
    /**
103
     * @return NodeInterface[]
104
     */
105
    public function getNodes(): ?array
106
    {
107
        return $this->nodes;
108
    }
109
110
    /**
111
     * @return bool
112
     */
113
    public function hasSource(): bool
114
    {
115
        return null !== $this->source;
116
    }
117
118
    /**
119
     * @return Source|null
120
     */
121
    public function getSource(): ?Source
122
    {
123
        return $this->source;
124
    }
125
126
    /**
127
     * @return int[]|null
128
     */
129
    public function getPositions(): ?array
130
    {
131
        return $this->positions;
132
    }
133
134
    /**
135
     * @return bool
136
     */
137
    public function hasLocations(): bool
138
    {
139
        return !empty($this->locations);
140
    }
141
142
    /**
143
     * @return array|null
144
     */
145
    public function getLocations(): ?array
146
    {
147
        return $this->locations;
148
    }
149
150
    /**
151
     * @return array|null
152
     */
153
    public function getLocationsAsArray(): ?array
154
    {
155
        return !empty($this->locations) ? array_map(function (SourceLocation $location) {
156
            return $location->toArray();
157
        }, $this->locations) : null;
158
    }
159
160
    /**
161
     * @return array|null
162
     */
163
    public function getPath(): ?array
164
    {
165
        return $this->path;
166
    }
167
168
    /**
169
     * @return \Exception|null
170
     */
171
    public function getOriginalException(): ?\Exception
172
    {
173
        return $this->originalException;
174
    }
175
176
    /**
177
     * @return null|string
178
     */
179
    public function getOriginalErrorMessage(): ?string
180
    {
181
        return null !== $this->originalException ? $this->originalException->getMessage() : null;
182
    }
183
184
    /**
185
     * @param array|null $nodes
186
     * @return $this
187
     */
188
    protected function resolveNodes(?array $nodes)
189
    {
190
        $this->nodes = is_array($nodes)
191
            ? (!empty($nodes) ? $nodes : null)
192
            : (null !== $nodes ? [$nodes] : null);
0 ignored issues
show
introduced by
The condition null !== $nodes is always false.
Loading history...
193
194
        return $this;
195
    }
196
197
    /**
198
     * @param Source|null $source
199
     * @return $this
200
     */
201
    protected function resolveSource(?Source $source)
202
    {
203
        if (null === $source && !empty($this->nodes)) {
204
            $firstNode = $this->nodes[0];
205
            $location  = null !== $firstNode ? $firstNode->getLocation() : null;
206
            $source    = null !== $location ? $location->getSource() : null;
207
        }
208
209
        $this->source = $source;
210
211
        return $this;
212
    }
213
214
    /**
215
     * @param array|null $positions
216
     * @return $this
217
     */
218
    protected function resolvePositions(?array $positions)
219
    {
220
        if (null === $positions && !empty($this->nodes)) {
221
            $positions = array_reduce($this->nodes, function (array $list, ?NodeInterface $node) {
222
                if (null !== $node) {
223
                    $location = $node->getLocation();
224
                    if (null !== $location) {
225
                        $list[] = $location->getStart();
226
                    }
227
                }
228
                return $list;
229
            }, []);
230
        }
231
232
        if (null !== $positions && empty($positions)) {
233
            $positions = null;
234
        }
235
236
        $this->positions = $positions;
237
238
        return $this;
239
    }
240
241
    /**
242
     * @param array|null  $positions
243
     * @param Source|null $source
244
     * @return $this
245
     */
246
    protected function resolveLocations(?array $positions, ?Source $source)
247
    {
248
        if (null !== $positions && null !== $source) {
249
            $locations = array_map(function ($position) use ($source) {
250
                return SourceLocation::fromSource($source, $position);
251
            }, $positions);
252
        } elseif (!empty($this->nodes)) {
253
            $locations = array_reduce($this->nodes, function (array $list, NodeInterface $node) {
254
                $location = $node->getLocation();
255
                if (null !== $location) {
256
                    $list[] = SourceLocation::fromSource($location->getSource(), $location->getStart());
257
                }
258
                return $list;
259
            }, []);
260
        }
261
262
        if (isset($locations)) {
263
            $this->locations = $locations;
264
        }
265
266
        return $this;
267
    }
268
269
    /**
270
     * @inheritdoc
271
     */
272
    public function toArray(): array
273
    {
274
        return [
275
            'message'   => $this->message,
276
            'locations' => $this->getLocationsAsArray(),
277
        ];
278
    }
279
280
    /**
281
     * @inheritdoc
282
     */
283
    public function __toString(): string
284
    {
285
        return printError($this);
286
    }
287
}
288