Skip to content

Commit 42d5cd5

Browse files
authored
feat: deprecate Error utils (#167)
1 parent 3a739f7 commit 42d5cd5

File tree

3 files changed

+2
-78
lines changed

3 files changed

+2
-78
lines changed

README.md

-78
Original file line numberDiff line numberDiff line change
@@ -263,84 +263,6 @@ $character = new UnionType(
263263
);
264264
```
265265

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-
344266
[GA Image]: https://github.com/simPod/GraphQL-Utils/workflows/CI/badge.svg
345267

346268
[GA Link]: https://github.com/simPod/GraphQL-Utils/actions?query=workflow%3A%22CI%22+branch%3A0.7.x

src/Error/Error.php

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace SimPod\GraphQLUtils\Error;
66

7+
/** @deprecated Use {@see ProvidesExtensions} */
78
abstract class Error extends \GraphQL\Error\Error
89
{
910
abstract public function getType(): string;

src/Error/FormattedError.php

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use GraphQL\Error\DebugFlag;
88
use Throwable;
99

10+
/** @deprecated Use {@see ProvidesExtensions} */
1011
class FormattedError extends \GraphQL\Error\FormattedError
1112
{
1213
public static function createFromException(Throwable $exception, int $debugFlag = DebugFlag::NONE, string|null $internalErrorMessage = null): array

0 commit comments

Comments
 (0)