Skip to content

feat: deprecate Error utils #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,13 +7,14 @@
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
{
$arrayError = parent::createFromException($exception, $debugFlag, $internalErrorMessage);

if ($exception instanceof \GraphQL\Error\Error && $exception->getPrevious() instanceof Error) {

Check warning on line 17 in src/Error/FormattedError.php

View workflow job for this annotation

GitHub Actions / Infection

Escaped Mutant for Mutator "InstanceOf_": @@ @@ public static function createFromException(Throwable $exception, int $debugFlag = DebugFlag::NONE, string|null $internalErrorMessage = null): array { $arrayError = parent::createFromException($exception, $debugFlag, $internalErrorMessage); - if ($exception instanceof \GraphQL\Error\Error && $exception->getPrevious() instanceof Error) { + if (true && $exception->getPrevious() instanceof Error) { $arrayError['extensions']['type'] = $exception->getPrevious()->getType(); } return $arrayError; } }

Check warning on line 17 in src/Error/FormattedError.php

View workflow job for this annotation

GitHub Actions / Infection

Escaped Mutant for Mutator "InstanceOf_": @@ @@ public static function createFromException(Throwable $exception, int $debugFlag = DebugFlag::NONE, string|null $internalErrorMessage = null): array { $arrayError = parent::createFromException($exception, $debugFlag, $internalErrorMessage); - if ($exception instanceof \GraphQL\Error\Error && $exception->getPrevious() instanceof Error) { + if ($exception instanceof \GraphQL\Error\Error && true) { $arrayError['extensions']['type'] = $exception->getPrevious()->getType(); } return $arrayError; } }

Check warning on line 17 in src/Error/FormattedError.php

View workflow job for this annotation

GitHub Actions / Infection

Escaped Mutant for Mutator "LogicalAnd": @@ @@ public static function createFromException(Throwable $exception, int $debugFlag = DebugFlag::NONE, string|null $internalErrorMessage = null): array { $arrayError = parent::createFromException($exception, $debugFlag, $internalErrorMessage); - if ($exception instanceof \GraphQL\Error\Error && $exception->getPrevious() instanceof Error) { + if ($exception instanceof \GraphQL\Error\Error || $exception->getPrevious() instanceof Error) { $arrayError['extensions']['type'] = $exception->getPrevious()->getType(); } return $arrayError; } }
$arrayError['extensions']['type'] = $exception->getPrevious()->getType();
}

Expand Down
Loading