Skip to content

Commit

Permalink
Merge 2786b9a into 652aed8
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Jun 25, 2019
2 parents 652aed8 + 2786b9a commit 77a46e9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
9 changes: 5 additions & 4 deletions Controller/GraphqliteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,15 @@ private function decideHttpStatusCode(ExecutionResult $result): int
$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 = 500): 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 77a46e9

Please sign in to comment.