Skip to content

Commit ee83926

Browse files
authored
Merge pull request #24 from moufmouf/error_code_from_exception_code
HTTP error code is not correctly fetched from exception code
2 parents 652aed8 + 3f7f2a9 commit ee83926

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

Controller/GraphqliteController.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,22 @@ private function handlePsr7Request(ServerRequestInterface $request): JsonRespons
117117
private function decideHttpStatusCode(ExecutionResult $result): int
118118
{
119119
// If the data entry in the response has any value other than null (when the operation has successfully executed without error) then the response should use the 200 (OK) status code.
120-
if ($result->data !== null) {
121-
return 200;
122-
}
123-
124-
if (empty($result->errors)) {
120+
if ($result->data !== null || empty($result->errors)) {
125121
return 200;
126122
}
127123

128124
$status = 0;
129125
// There might be many errors. Let's return the highest code we encounter.
130126
foreach ($result->errors as $error) {
131-
if ($error->getCategory() === Error::CATEGORY_GRAPHQL) {
132-
$code = 400;
133-
} else {
134-
$code = $error->getCode();
127+
$wrappedException = $error->getPrevious();
128+
if ($wrappedException !== null) {
129+
$code = $wrappedException->getCode();
135130
if (!isset(Response::$statusTexts[$code])) {
136131
// The exception code is not a valid HTTP code. Let's ignore it
137132
continue;
138133
}
134+
} else {
135+
$code = 400;
139136
}
140137
$status = max($status, $code);
141138
}

Tests/Fixtures/Controller/TestGraphqlController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace TheCodingMachine\Graphqlite\Bundle\Tests\Fixtures\Controller;
55

66

7+
use GraphQL\Error\Error;
78
use Porpaginas\Arrays\ArrayResult;
89
use TheCodingMachine\Graphqlite\Bundle\Tests\Fixtures\Entities\Contact;
910
use TheCodingMachine\Graphqlite\Bundle\Tests\Fixtures\Entities\Product;
@@ -61,8 +62,8 @@ public function contacts(): ArrayResult
6162
* @Query()
6263
* @return string
6364
*/
64-
public function triggerError(): string
65+
public function triggerException(int $code = 0): string
6566
{
66-
throw new MyException('Boom');
67+
throw new MyException('Boom', $code);
6768
}
6869
}

Tests/FunctionalTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,23 @@ public function testErrors()
106106

107107
$request = Request::create('/graphql', 'GET', ['query' => '
108108
{
109-
triggerError
109+
triggerException
110110
}']);
111111

112112
$response = $kernel->handle($request);
113113

114114
$this->assertSame(500, $response->getStatusCode());
115115

116+
// Let's test that the highest exception code compatible with an HTTP is kept.
117+
$request = Request::create('/graphql', 'GET', ['query' => '
118+
{
119+
triggerError1: triggerException(code: 404)
120+
triggerError2: triggerException(code: 401)
121+
triggerError3: triggerException(code: 10245)
122+
}']);
123+
124+
$response = $kernel->handle($request);
125+
126+
$this->assertSame(404, $response->getStatusCode(), $response->getContent());
116127
}
117128
}

0 commit comments

Comments
 (0)