1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Tests\Error; |
4
|
|
|
|
5
|
|
|
use GraphQL\Error\Error as GraphQLError; |
6
|
|
|
use GraphQL\Error\UserError as GraphQLUserError; |
7
|
|
|
use GraphQL\Executor\ExecutionResult; |
8
|
|
|
use GraphQL\GraphQL; |
9
|
|
|
use Overblog\GraphQLBundle\Error\ErrorHandler; |
10
|
|
|
use Overblog\GraphQLBundle\Error\UserError; |
11
|
|
|
use Overblog\GraphQLBundle\Error\UserErrors; |
12
|
|
|
use Overblog\GraphQLBundle\Error\UserWarning; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
class ErrorHandlerTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** @var ErrorHandler */ |
18
|
|
|
private $errorHandler; |
19
|
|
|
|
20
|
|
|
public function setUp() |
21
|
|
|
{ |
22
|
|
|
$this->errorHandler = new ErrorHandler(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testMaskErrorWithThrowExceptionSetToFalse() |
26
|
|
|
{ |
27
|
|
|
$executionResult = new ExecutionResult( |
28
|
|
|
null, |
29
|
|
|
[ |
30
|
|
|
new GraphQLError('Error without wrapped exception'), |
31
|
|
|
new GraphQLError('Error with wrapped exception', null, null, null, null, new \Exception('My Exception message')), |
|
|
|
|
32
|
|
|
new GraphQLError('Error with wrapped user error', null, null, null, null, new UserError('My User Error')), |
|
|
|
|
33
|
|
|
new GraphQLError('', null, null, null, null, new UserErrors(['My User Error 1', 'My User Error 2', new UserError('My User Error 3')])), |
|
|
|
|
34
|
|
|
new GraphQLError('Error with wrapped user warning', null, null, null, null, new UserWarning('My User Warning')), |
|
|
|
|
35
|
|
|
new GraphQLError('Invalid value!', null, null, null, null, new GraphQLUserError('Invalid value!')), |
|
|
|
|
36
|
|
|
] |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
$this->errorHandler->handleErrors($executionResult); |
40
|
|
|
|
41
|
|
|
$expected = [ |
42
|
|
|
'errors' => [ |
43
|
|
|
[ |
44
|
|
|
'message' => 'Error without wrapped exception', |
45
|
|
|
'category' => 'graphql', |
46
|
|
|
], |
47
|
|
|
[ |
48
|
|
|
'message' => ErrorHandler::DEFAULT_ERROR_MESSAGE, |
49
|
|
|
'category' => 'internal', |
50
|
|
|
], |
51
|
|
|
[ |
52
|
|
|
'message' => 'Error with wrapped user error', |
53
|
|
|
'category' => 'user', |
54
|
|
|
], |
55
|
|
|
[ |
56
|
|
|
'message' => 'My User Error 1', |
57
|
|
|
'category' => 'user', |
58
|
|
|
], |
59
|
|
|
[ |
60
|
|
|
'message' => 'My User Error 2', |
61
|
|
|
'category' => 'user', |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
'message' => 'My User Error 3', |
65
|
|
|
'category' => 'user', |
66
|
|
|
], |
67
|
|
|
[ |
68
|
|
|
'message' => 'Invalid value!', |
69
|
|
|
'category' => 'user', |
70
|
|
|
], |
71
|
|
|
], |
72
|
|
|
'extensions' => [ |
73
|
|
|
'warnings' => [ |
74
|
|
|
[ |
75
|
|
|
'message' => 'Error with wrapped user warning', |
76
|
|
|
'category' => 'user', |
77
|
|
|
], |
78
|
|
|
], |
79
|
|
|
], |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testMaskErrorWithWrappedExceptionAndThrowExceptionSetToTrue() |
86
|
|
|
{ |
87
|
|
|
$this->expectException(\Exception::class); |
88
|
|
|
$this->expectExceptionMessage('My Exception message'); |
89
|
|
|
|
90
|
|
|
$executionResult = new ExecutionResult( |
91
|
|
|
null, |
92
|
|
|
[ |
93
|
|
|
new GraphQLError('Error with wrapped exception', null, null, null, null, new \Exception('My Exception message')), |
|
|
|
|
94
|
|
|
] |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$this->errorHandler->handleErrors($executionResult, true); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testInvalidUserErrorsItem() |
101
|
|
|
{ |
102
|
|
|
$this->expectException(\InvalidArgumentException::class); |
103
|
|
|
$this->expectExceptionMessage('Error must be string or instance of Overblog\GraphQLBundle\Error\UserError.'); |
104
|
|
|
|
105
|
|
|
new UserErrors([ |
106
|
|
|
'Some Error', |
107
|
|
|
false, |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testMaskErrorWithWrappedUserErrorAndThrowExceptionSetToTrue() |
112
|
|
|
{ |
113
|
|
|
$executionResult = new ExecutionResult( |
114
|
|
|
null, |
115
|
|
|
[ |
116
|
|
|
new GraphQLError('Error with wrapped user error', null, null, null, null, new UserError('My User Error')), |
|
|
|
|
117
|
|
|
] |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$this->errorHandler->handleErrors($executionResult, true); |
121
|
|
|
|
122
|
|
|
$expected = [ |
123
|
|
|
'errors' => [ |
124
|
|
|
[ |
125
|
|
|
'message' => 'Error with wrapped user error', |
126
|
|
|
'category' => 'user', |
127
|
|
|
], |
128
|
|
|
], |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testMaskErrorWithoutWrappedExceptionAndThrowExceptionSetToTrue() |
135
|
|
|
{ |
136
|
|
|
$executionResult = new ExecutionResult( |
137
|
|
|
null, |
138
|
|
|
[ |
139
|
|
|
new GraphQLError('Error without wrapped exception'), |
140
|
|
|
] |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$this->errorHandler->handleErrors($executionResult, true); |
144
|
|
|
|
145
|
|
|
$expected = [ |
146
|
|
|
'errors' => [ |
147
|
|
|
[ |
148
|
|
|
'message' => 'Error without wrapped exception', |
149
|
|
|
'category' => 'graphql', |
150
|
|
|
], |
151
|
|
|
], |
152
|
|
|
]; |
153
|
|
|
|
154
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testConvertExceptionToUserWarning() |
158
|
|
|
{ |
159
|
|
|
$errorHandler = new ErrorHandler(null, null, [\InvalidArgumentException::class => UserWarning::class]); |
160
|
|
|
|
161
|
|
|
$executionResult = new ExecutionResult( |
162
|
|
|
null, |
163
|
|
|
[ |
164
|
|
|
new GraphQLError('Error with invalid argument exception', null, null, null, null, new \InvalidArgumentException('Invalid argument exception')), |
|
|
|
|
165
|
|
|
] |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
$errorHandler->handleErrors($executionResult, true); |
169
|
|
|
|
170
|
|
|
$expected = [ |
171
|
|
|
'extensions' => [ |
172
|
|
|
'warnings' => [ |
173
|
|
|
[ |
174
|
|
|
'message' => 'Error with invalid argument exception', |
175
|
|
|
'category' => 'user', |
176
|
|
|
], |
177
|
|
|
], |
178
|
|
|
], |
179
|
|
|
]; |
180
|
|
|
|
181
|
|
|
$this->assertEquals($expected, $executionResult->toArray()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param array $exceptionMap |
186
|
|
|
* @param bool $mapExceptionsToParent |
187
|
|
|
* @param array|string $expectedUserError |
188
|
|
|
* |
189
|
|
|
* @dataProvider parentExceptionMappingDataProvider |
190
|
|
|
*/ |
191
|
|
|
public function testConvertExceptionUsingParentExceptionMatchesAlwaysFirstExactExceptionOtherwiseMatchesParent(array $exceptionMap, $mapExceptionsToParent, $expectedUserError) |
192
|
|
|
{ |
193
|
|
|
$errorHandler = new ErrorHandler(null, null, $exceptionMap, $mapExceptionsToParent); |
194
|
|
|
|
195
|
|
|
$executionResult = new ExecutionResult( |
196
|
|
|
null, |
197
|
|
|
[ |
198
|
|
|
new GraphQLError( |
199
|
|
|
'Error with invalid argument exception', |
200
|
|
|
null, |
201
|
|
|
null, |
202
|
|
|
null, |
203
|
|
|
null, |
204
|
|
|
new ChildOfInvalidArgumentException('Invalid argument exception') |
|
|
|
|
205
|
|
|
), |
206
|
|
|
] |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
if (is_string($expectedUserError)) { |
210
|
|
|
self::expectException($expectedUserError); |
211
|
|
|
} |
212
|
|
|
$errorHandler->handleErrors($executionResult, true); |
213
|
|
|
|
214
|
|
|
if (is_array($expectedUserError)) { |
215
|
|
|
$this->assertEquals($expectedUserError, $executionResult->toArray()); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function testCustomFormatterError() |
220
|
|
|
{ |
221
|
|
|
$executionResult = new ExecutionResult( |
222
|
|
|
null, |
223
|
|
|
[ |
224
|
|
|
new GraphQLError('Error with wrapped exception', null, null, null, null, new \Exception('My Exception message', 123)), |
|
|
|
|
225
|
|
|
] |
226
|
|
|
); |
227
|
|
|
|
228
|
|
|
$this->errorHandler->setErrorFormatter(new CustomErrorFormatter()); |
229
|
|
|
|
230
|
|
|
$this->errorHandler->handleErrors($executionResult); |
231
|
|
|
$this->assertEquals([ |
232
|
|
|
'errors' => [ |
233
|
|
|
[ |
234
|
|
|
'message' => 'Internal server Error', |
235
|
|
|
'category' => 'internal', |
236
|
|
|
'code' => 123, |
237
|
|
|
], |
238
|
|
|
], |
239
|
|
|
], $executionResult->toArray()); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return array |
244
|
|
|
*/ |
245
|
|
|
public function parentExceptionMappingDataProvider() |
246
|
|
|
{ |
247
|
|
|
return [ |
248
|
|
|
'without $mapExceptionsToParent and only the exact class, maps to exact class' => [ |
249
|
|
|
[ |
250
|
|
|
ChildOfInvalidArgumentException::class => UserError::class, |
251
|
|
|
], |
252
|
|
|
false, |
253
|
|
|
[ |
254
|
|
|
'errors' => [ |
255
|
|
|
[ |
256
|
|
|
'message' => 'Error with invalid argument exception', |
257
|
|
|
'category' => 'user', |
258
|
|
|
], |
259
|
|
|
], |
260
|
|
|
], |
261
|
|
|
], |
262
|
|
|
'without $mapExceptionsToParent and only the parent class, does not map to parent' => [ |
263
|
|
|
[ |
264
|
|
|
\InvalidArgumentException::class => UserWarning::class, |
265
|
|
|
], |
266
|
|
|
false, |
267
|
|
|
ChildOfInvalidArgumentException::class, |
268
|
|
|
], |
269
|
|
|
'with $mapExceptionsToParent and no classes' => [ |
270
|
|
|
[], |
271
|
|
|
true, |
272
|
|
|
ChildOfInvalidArgumentException::class, |
273
|
|
|
], |
274
|
|
|
'with $mapExceptionsToParent and only the exact class' => [ |
275
|
|
|
[ |
276
|
|
|
ChildOfInvalidArgumentException::class => UserError::class, |
277
|
|
|
], |
278
|
|
|
true, |
279
|
|
|
[ |
280
|
|
|
'errors' => [ |
281
|
|
|
[ |
282
|
|
|
'message' => 'Error with invalid argument exception', |
283
|
|
|
'category' => 'user', |
284
|
|
|
], |
285
|
|
|
], |
286
|
|
|
], |
287
|
|
|
], |
288
|
|
|
'with $mapExceptionsToParent and only the parent class' => [ |
289
|
|
|
[ |
290
|
|
|
\InvalidArgumentException::class => UserWarning::class, |
291
|
|
|
], |
292
|
|
|
true, |
293
|
|
|
[ |
294
|
|
|
'extensions' => [ |
295
|
|
|
'warnings' => [ |
296
|
|
|
[ |
297
|
|
|
'message' => 'Error with invalid argument exception', |
298
|
|
|
'category' => 'user', |
299
|
|
|
], |
300
|
|
|
], |
301
|
|
|
], |
302
|
|
|
], |
303
|
|
|
], |
304
|
|
|
'with $mapExceptionsToParent and the exact class first matches exact class' => [ |
305
|
|
|
[ |
306
|
|
|
ChildOfInvalidArgumentException::class => UserError::class, |
307
|
|
|
\InvalidArgumentException::class => UserWarning::class, |
308
|
|
|
], |
309
|
|
|
true, |
310
|
|
|
[ |
311
|
|
|
'errors' => [ |
312
|
|
|
[ |
313
|
|
|
'message' => 'Error with invalid argument exception', |
314
|
|
|
'category' => 'user', |
315
|
|
|
], |
316
|
|
|
], |
317
|
|
|
], |
318
|
|
|
], |
319
|
|
|
'with $mapExceptionsToParent and the exact class first but parent maps to error' => [ |
320
|
|
|
[ |
321
|
|
|
ChildOfInvalidArgumentException::class => UserWarning::class, |
322
|
|
|
\InvalidArgumentException::class => UserError::class, |
323
|
|
|
], |
324
|
|
|
true, |
325
|
|
|
[ |
326
|
|
|
'extensions' => [ |
327
|
|
|
'warnings' => [ |
328
|
|
|
[ |
329
|
|
|
'message' => 'Error with invalid argument exception', |
330
|
|
|
'category' => 'user', |
331
|
|
|
], |
332
|
|
|
], |
333
|
|
|
], |
334
|
|
|
], |
335
|
|
|
], |
336
|
|
|
'with $mapExceptionsToParent and the parent class first still matches exact class' => [ |
337
|
|
|
[ |
338
|
|
|
\InvalidArgumentException::class => UserWarning::class, |
339
|
|
|
ChildOfInvalidArgumentException::class => UserError::class, |
340
|
|
|
], |
341
|
|
|
true, |
342
|
|
|
[ |
343
|
|
|
'errors' => [ |
344
|
|
|
[ |
345
|
|
|
'message' => 'Error with invalid argument exception', |
346
|
|
|
'category' => 'user', |
347
|
|
|
], |
348
|
|
|
], |
349
|
|
|
], |
350
|
|
|
], |
351
|
|
|
]; |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: