Skip to content

Commit

Permalink
Merge 3f7f2a9 into 652aed8
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Jun 25, 2019
2 parents 652aed8 + 3f7f2a9 commit 7075570
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
15 changes: 6 additions & 9 deletions Controller/GraphqliteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,22 @@ private function handlePsr7Request(ServerRequestInterface $request): JsonRespons
private function decideHttpStatusCode(ExecutionResult $result): int
{
// 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.
if ($result->data !== null) {
return 200;
}

if (empty($result->errors)) {
if ($result->data !== null || empty($result->errors)) {
return 200;
}

$status = 0;
// There might be many errors. Let's return the highest code we encounter.
foreach ($result->errors as $error) {
if ($error->getCategory() === Error::CATEGORY_GRAPHQL) {
$code = 400;
} else {
$code = $error->getCode();
$wrappedException = $error->getPrevious();
if ($wrappedException !== null) {
$code = $wrappedException->getCode();
if (!isset(Response::$statusTexts[$code])) {
// The exception code is not a valid HTTP code. Let's ignore it
continue;
}
} else {
$code = 400;
}
$status = max($status, $code);
}
Expand Down
5 changes: 3 additions & 2 deletions Tests/Fixtures/Controller/TestGraphqlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace TheCodingMachine\Graphqlite\Bundle\Tests\Fixtures\Controller;


use GraphQL\Error\Error;
use Porpaginas\Arrays\ArrayResult;
use TheCodingMachine\Graphqlite\Bundle\Tests\Fixtures\Entities\Contact;
use TheCodingMachine\Graphqlite\Bundle\Tests\Fixtures\Entities\Product;
Expand Down Expand Up @@ -61,8 +62,8 @@ public function contacts(): ArrayResult
* @Query()
* @return string
*/
public function triggerError(): string
public function triggerException(int $code = 0): string
{
throw new MyException('Boom');
throw new MyException('Boom', $code);
}
}
13 changes: 12 additions & 1 deletion Tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,23 @@ public function testErrors()

$request = Request::create('/graphql', 'GET', ['query' => '
{
triggerError
triggerException
}']);

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

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

// Let's test that the highest exception code compatible with an HTTP is kept.
$request = Request::create('/graphql', 'GET', ['query' => '
{
triggerError1: triggerException(code: 404)
triggerError2: triggerException(code: 401)
triggerError3: triggerException(code: 10245)
}']);

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

$this->assertSame(404, $response->getStatusCode(), $response->getContent());
}
}

0 comments on commit 7075570

Please sign in to comment.