Skip to content

Commit

Permalink
feat: deprecate Error utils
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Jul 27, 2024
1 parent 0e42b2f commit ced09ea
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 78 deletions.
78 changes: 0 additions & 78 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,84 +263,6 @@ $character = new UnionType(
);
```

### Error Handling

Extending your exception with `SimPod\GraphQLUtils\Error\Error` forces you to implement `getType()` method.

Example Error class

```php
<?php

use SimPod\GraphQLUtils\Error\Error;

final class InvalidCustomerIdProvided extends Error
{
private const TYPE = 'INVALID_CUSTOMER_ID_PROVIDED';

public static function noneGiven() : self
{
return new self('No CustomerId provided');
}

public function getType() : string
{
return self::TYPE;
}

public function isClientSafe() : bool
{
return true;
}
}
```

Create your formatter

```php
<?php

use GraphQL\Error\Error;
use SimPod\GraphQLUtils\Error\FormattedError;

$formatError = static function (Error $error) : array
{
if (! $error->isClientSafe()) {
// eg. log error
}

return FormattedError::createFromException($error);
};

$errorFormatterCallback = static function (Error $error) use ($formatError) : array {
return $formatError($error);
};

$config = GraphQL::executeQuery(/* $args */)
->setErrorFormatter($errorFormatterCallback)
->setErrorsHandler(
static function (array $errors, callable $formatter) : array {
return array_map($formatter, $errors);
}
);
```

Error types will then be provided in your response so client can easier identify the error type

```json
{
"errors": [
{
"message": "No CustomerId provided",
"extensions": {
"type": "INVALID_CUSTOMER_ID_PROVIDED",
"category": "validation"
}
}
]
}
```

[GA Image]: https://github.com/simPod/GraphQL-Utils/workflows/CI/badge.svg

[GA Link]: https://github.com/simPod/GraphQL-Utils/actions?query=workflow%3A%22CI%22+branch%3A0.7.x
Expand Down
1 change: 1 addition & 0 deletions src/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SimPod\GraphQLUtils\Error;

/** @deprecated Use {@see ProvidesExtensions} */
abstract class Error extends \GraphQL\Error\Error
{
abstract public function getType(): string;
Expand Down
1 change: 1 addition & 0 deletions src/Error/FormattedError.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GraphQL\Error\DebugFlag;
use Throwable;

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

0 comments on commit ced09ea

Please sign in to comment.