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