Skip to content

[Error handling] Remove category from exceptions #685

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 3 commits into from
May 9, 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
11 changes: 0 additions & 11 deletions src/Exceptions/GraphQLException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public function __construct(
string $message,
int $code = 0,
Throwable|null $previous = null,
protected string $category = 'Exception',
protected array $extensions = [],
) {
parent::__construct($message, $code, $previous);
Expand All @@ -28,16 +27,6 @@ public function isClientSafe(): bool
return true;
}

/**
* Returns string describing a category of the error.
*
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
*/
public function getCategory(): string
{
return $this->category;
}

/**
* Returns the "extensions" object attached to the GraphQL error.
*
Expand Down
10 changes: 0 additions & 10 deletions src/Mappers/PorpaginasMissingParameterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,4 @@ public function isClientSafe(): bool
{
return true;
}

/**
* Returns string describing a category of the error.
*
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
*/
public function getCategory(): string
{
return 'pagination';
}
}
10 changes: 0 additions & 10 deletions src/Middlewares/MissingAuthorizationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,4 @@ public function isClientSafe(): bool
{
return true;
}

/**
* Returns string describing a category of the error.
*
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
*/
public function getCategory(): string
{
return 'security';
}
}
10 changes: 0 additions & 10 deletions src/Parameters/MissingArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,6 @@ public function isClientSafe(): bool
return true;
}

/**
* Returns string describing a category of the error.
*
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
*/
public function getCategory(): string
{
return 'graphql';
}

/**
* Returns the "extensions" object attached to the GraphQL error.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/Exceptions/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ErrorHandlerTest extends TestCase

public function testErrorFormatter()
{
$exception = new GraphQLException('foo', 0, null, 'MyCategory', ['field' => 'foo']);
$exception = new GraphQLException('foo', 0, null, ['field' => 'foo']);
$error = new Error('foo', null, null, [], null, $exception);
$formattedError = WebonyxErrorHandler::errorFormatter($error);
$this->assertSame([
Expand All @@ -23,7 +23,7 @@ public function testErrorFormatter()

public function testErrorHandler()
{
$exception = new GraphQLException('foo', 0, null, 'MyCategory', ['field' => 'foo']);
$exception = new GraphQLException('foo', 0, null, ['field' => 'foo']);
$error = new Error('bar', null, null, [], null, $exception);
$aggregateException = new GraphQLAggregateException();
$aggregateException->add($exception);
Expand Down
5 changes: 0 additions & 5 deletions tests/Http/HttpCodeDeciderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ public function isClientSafe():bool
{
return true;
}

public function getCategory()
{
return 'foo';
}
};
$clientAwareError = new Error('Foo', null, null, [], null, $clientAwareException);

Expand Down
1 change: 0 additions & 1 deletion tests/Parameters/MissingArgumentExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ public function testWrapWithFactoryContext(): void

$this->assertTrue($e->isClientSafe());
$this->assertSame([], $e->getExtensions());
$this->assertSame('graphql', $e->getCategory());
}
}
40 changes: 1 addition & 39 deletions website/docs/error-handling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ In GraphQL, when an error occurs, the server must add an "error" entry in the re
{
"message": "Name for character with ID 1002 could not be fetched.",
"locations": [ { "line": 6, "column": 7 } ],
"path": [ "hero", "heroFriends", 1, "name" ],
"extensions": {
"category": "Exception"
}
"path": [ "hero", "heroFriends", 1, "name" ]
}
]
}
Expand Down Expand Up @@ -43,30 +40,6 @@ throw new GraphQLException("Not found", 404);
<div class="alert alert--info">GraphQL allows to have several errors for one request. If you have several
<code>GraphQLException</code> thrown for the same request, the HTTP status code used will be the highest one.</div>

## Customizing the category

By default, GraphQLite adds a "category" entry in the "extensions section". You can customize the category with the
4th parameter of the constructor:

```php
throw new GraphQLException("Not found", 404, null, "NOT_FOUND");
```

will generate:

```json
{
"errors": [
{
"message": "Not found",
"extensions": {
"category": "NOT_FOUND"
}
}
]
}
```

## Customizing the extensions section

You can customize the whole "extensions" section with the 5th parameter of the constructor:
Expand All @@ -83,7 +56,6 @@ will generate:
{
"message": "Field required",
"extensions": {
"category": "VALIDATION",
"field": "name"
}
}
Expand All @@ -109,16 +81,6 @@ class ValidationException extends Exception implements GraphQLExceptionInterface
return true;
}

/**
* Returns string describing a category of the error.
*
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
*/
public function getCategory(): string
{
return 'VALIDATION';
}

/**
* Returns the "extensions" object attached to the GraphQL error.
*
Expand Down
6 changes: 2 additions & 4 deletions website/docs/laravel-package-advanced.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ If a validation fails to pass, the message will be printed in the "errors" secti
{
"message": "The email must be a valid email address.",
"extensions": {
"argument": "email",
"category": "Validate"
"argument": "email"
}
},
{
"message": "The password must be greater than or equal 8 characters.",
"extensions": {
"argument": "password",
"category": "Validate"
"argument": "password"
}
}
]
Expand Down
3 changes: 1 addition & 2 deletions website/docs/validation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ If a validation fails, GraphQLite will return the failed validations in the "err
"message": "The email '\"[email protected]\"' is not a valid email.",
"extensions": {
"code": "bf447c1c-0266-4e10-9c6c-573df282e413",
"field": "email",
"category": "Validate"
"field": "email"
}
}
]
Expand Down
Loading