Skip to content

Commit c4a68a8

Browse files
authored
[Error handling] Remove category from exceptions (thecodingmachine#685)
* Remove category from exceptions * Remove category from tests * Remove category from docs
1 parent 1a8dd39 commit c4a68a8

10 files changed

+6
-94
lines changed

src/Exceptions/GraphQLException.php

-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public function __construct(
1414
string $message,
1515
int $code = 0,
1616
Throwable|null $previous = null,
17-
protected string $category = 'Exception',
1817
protected array $extensions = [],
1918
) {
2019
parent::__construct($message, $code, $previous);
@@ -28,16 +27,6 @@ public function isClientSafe(): bool
2827
return true;
2928
}
3029

31-
/**
32-
* Returns string describing a category of the error.
33-
*
34-
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
35-
*/
36-
public function getCategory(): string
37-
{
38-
return $this->category;
39-
}
40-
4130
/**
4231
* Returns the "extensions" object attached to the GraphQL error.
4332
*

src/Mappers/PorpaginasMissingParameterException.php

-10
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,4 @@ public function isClientSafe(): bool
2828
{
2929
return true;
3030
}
31-
32-
/**
33-
* Returns string describing a category of the error.
34-
*
35-
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
36-
*/
37-
public function getCategory(): string
38-
{
39-
return 'pagination';
40-
}
4131
}

src/Middlewares/MissingAuthorizationException.php

-10
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,4 @@ public function isClientSafe(): bool
2626
{
2727
return true;
2828
}
29-
30-
/**
31-
* Returns string describing a category of the error.
32-
*
33-
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
34-
*/
35-
public function getCategory(): string
36-
{
37-
return 'security';
38-
}
3929
}

src/Parameters/MissingArgumentException.php

-10
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,6 @@ public function isClientSafe(): bool
8181
return true;
8282
}
8383

84-
/**
85-
* Returns string describing a category of the error.
86-
*
87-
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
88-
*/
89-
public function getCategory(): string
90-
{
91-
return 'graphql';
92-
}
93-
9484
/**
9585
* Returns the "extensions" object attached to the GraphQL error.
9686
*

tests/Exceptions/ErrorHandlerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ErrorHandlerTest extends TestCase
1010

1111
public function testErrorFormatter()
1212
{
13-
$exception = new GraphQLException('foo', 0, null, 'MyCategory', ['field' => 'foo']);
13+
$exception = new GraphQLException('foo', 0, null, ['field' => 'foo']);
1414
$error = new Error('foo', null, null, [], null, $exception);
1515
$formattedError = WebonyxErrorHandler::errorFormatter($error);
1616
$this->assertSame([
@@ -23,7 +23,7 @@ public function testErrorFormatter()
2323

2424
public function testErrorHandler()
2525
{
26-
$exception = new GraphQLException('foo', 0, null, 'MyCategory', ['field' => 'foo']);
26+
$exception = new GraphQLException('foo', 0, null, ['field' => 'foo']);
2727
$error = new Error('bar', null, null, [], null, $exception);
2828
$aggregateException = new GraphQLAggregateException();
2929
$aggregateException->add($exception);

tests/Http/HttpCodeDeciderTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ public function isClientSafe():bool
3838
{
3939
return true;
4040
}
41-
42-
public function getCategory()
43-
{
44-
return 'foo';
45-
}
4641
};
4742
$clientAwareError = new Error('Foo', null, null, [], null, $clientAwareException);
4843

tests/Parameters/MissingArgumentExceptionTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ public function testWrapWithFactoryContext(): void
2121

2222
$this->assertTrue($e->isClientSafe());
2323
$this->assertSame([], $e->getExtensions());
24-
$this->assertSame('graphql', $e->getCategory());
2524
}
2625
}

website/docs/error-handling.mdx

+1-39
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ In GraphQL, when an error occurs, the server must add an "error" entry in the re
1212
{
1313
"message": "Name for character with ID 1002 could not be fetched.",
1414
"locations": [ { "line": 6, "column": 7 } ],
15-
"path": [ "hero", "heroFriends", 1, "name" ],
16-
"extensions": {
17-
"category": "Exception"
18-
}
15+
"path": [ "hero", "heroFriends", 1, "name" ]
1916
}
2017
]
2118
}
@@ -43,30 +40,6 @@ throw new GraphQLException("Not found", 404);
4340
<div class="alert alert--info">GraphQL allows to have several errors for one request. If you have several
4441
<code>GraphQLException</code> thrown for the same request, the HTTP status code used will be the highest one.</div>
4542

46-
## Customizing the category
47-
48-
By default, GraphQLite adds a "category" entry in the "extensions section". You can customize the category with the
49-
4th parameter of the constructor:
50-
51-
```php
52-
throw new GraphQLException("Not found", 404, null, "NOT_FOUND");
53-
```
54-
55-
will generate:
56-
57-
```json
58-
{
59-
"errors": [
60-
{
61-
"message": "Not found",
62-
"extensions": {
63-
"category": "NOT_FOUND"
64-
}
65-
}
66-
]
67-
}
68-
```
69-
7043
## Customizing the extensions section
7144

7245
You can customize the whole "extensions" section with the 5th parameter of the constructor:
@@ -83,7 +56,6 @@ will generate:
8356
{
8457
"message": "Field required",
8558
"extensions": {
86-
"category": "VALIDATION",
8759
"field": "name"
8860
}
8961
}
@@ -109,16 +81,6 @@ class ValidationException extends Exception implements GraphQLExceptionInterface
10981
return true;
11082
}
11183

112-
/**
113-
* Returns string describing a category of the error.
114-
*
115-
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
116-
*/
117-
public function getCategory(): string
118-
{
119-
return 'VALIDATION';
120-
}
121-
12284
/**
12385
* Returns the "extensions" object attached to the GraphQL error.
12486
*

website/docs/laravel-package-advanced.mdx

+2-4
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,13 @@ If a validation fails to pass, the message will be printed in the "errors" secti
4343
{
4444
"message": "The email must be a valid email address.",
4545
"extensions": {
46-
"argument": "email",
47-
"category": "Validate"
46+
"argument": "email"
4847
}
4948
},
5049
{
5150
"message": "The password must be greater than or equal 8 characters.",
5251
"extensions": {
53-
"argument": "password",
54-
"category": "Validate"
52+
"argument": "password"
5553
}
5654
}
5755
]

website/docs/validation.mdx

+1-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ If a validation fails, GraphQLite will return the failed validations in the "err
9696
"message": "The email '\"[email protected]\"' is not a valid email.",
9797
"extensions": {
9898
"code": "bf447c1c-0266-4e10-9c6c-573df282e413",
99-
"field": "email",
100-
"category": "Validate"
99+
"field": "email"
101100
}
102101
}
103102
]

0 commit comments

Comments
 (0)