@@ -263,84 +263,6 @@ $character = new UnionType(
263
263
);
264
264
```
265
265
266
- ### Error Handling
267
-
268
- Extending your exception with ` SimPod\GraphQLUtils\Error\Error ` forces you to implement ` getType() ` method.
269
-
270
- Example Error class
271
-
272
- ``` php
273
- <?php
274
-
275
- use SimPod\GraphQLUtils\Error\Error;
276
-
277
- final class InvalidCustomerIdProvided extends Error
278
- {
279
- private const TYPE = 'INVALID_CUSTOMER_ID_PROVIDED';
280
-
281
- public static function noneGiven() : self
282
- {
283
- return new self('No CustomerId provided');
284
- }
285
-
286
- public function getType() : string
287
- {
288
- return self::TYPE;
289
- }
290
-
291
- public function isClientSafe() : bool
292
- {
293
- return true;
294
- }
295
- }
296
- ```
297
-
298
- Create your formatter
299
-
300
- ``` php
301
- <?php
302
-
303
- use GraphQL\Error\Error;
304
- use SimPod\GraphQLUtils\Error\FormattedError;
305
-
306
- $formatError = static function (Error $error) : array
307
- {
308
- if (! $error->isClientSafe()) {
309
- // eg. log error
310
- }
311
-
312
- return FormattedError::createFromException($error);
313
- };
314
-
315
- $errorFormatterCallback = static function (Error $error) use ($formatError) : array {
316
- return $formatError($error);
317
- };
318
-
319
- $config = GraphQL::executeQuery(/* $args */)
320
- ->setErrorFormatter($errorFormatterCallback)
321
- ->setErrorsHandler(
322
- static function (array $errors, callable $formatter) : array {
323
- return array_map($formatter, $errors);
324
- }
325
- );
326
- ```
327
-
328
- Error types will then be provided in your response so client can easier identify the error type
329
-
330
- ``` json
331
- {
332
- "errors" : [
333
- {
334
- "message" : " No CustomerId provided" ,
335
- "extensions" : {
336
- "type" : " INVALID_CUSTOMER_ID_PROVIDED" ,
337
- "category" : " validation"
338
- }
339
- }
340
- ]
341
- }
342
- ```
343
-
344
266
[ GA Image ] : https://github.com/simPod/GraphQL-Utils/workflows/CI/badge.svg
345
267
346
268
[ GA Link ] : https://github.com/simPod/GraphQL-Utils/actions?query=workflow%3A%22CI%22+branch%3A0.7.x
0 commit comments