Skip to content

Commit 022e0b6

Browse files
authored
Merge pull request #206 from moufmouf/http400forclientaware
ClientAware exceptions now trigger HTTP 400 errors
2 parents f79ade6 + f35d6fa commit 022e0b6

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/Http/HttpCodeDecider.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace TheCodingMachine\GraphQLite\Http;
66

7+
use GraphQL\Error\ClientAware;
78
use GraphQL\Executor\ExecutionResult;
89
use function max;
910

@@ -28,8 +29,15 @@ public function decideHttpStatusCode(ExecutionResult $result): int
2829
if ($wrappedException !== null) {
2930
$code = $wrappedException->getCode();
3031
if ($code < 400 || $code >= 600) {
31-
// The exception code is not a valid HTTP code. Let's ignore it
32-
continue;
32+
if (! ($wrappedException instanceof ClientAware) || $wrappedException->isClientSafe() !== true) {
33+
// The exception code is not a valid HTTP code. Let's ignore it
34+
continue;
35+
}
36+
37+
// A "client aware" exception is almost certainly targeting the client (there is
38+
// no need to pass a server exception error message to the client).
39+
// So a ClientAware exception is almost certainly a HTTP 400 code
40+
$code = 400;
3341
}
3442
} else {
3543
$code = 400;

tests/Http/HttpCodeDeciderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace TheCodingMachine\GraphQLite\Http;
44

55
use Exception;
6+
use GraphQL\Error\ClientAware;
67
use GraphQL\Error\Error;
78
use GraphQL\Executor\ExecutionResult;
89
use PHPUnit\Framework\TestCase;
@@ -32,6 +33,19 @@ public function testDecideHttpStatusCode(): void
3233
$exception600 = new Exception('foo', 600);
3334
$errorCode600 = new Error('Foo', null, null, null, null, $exception600);
3435

36+
$clientAwareException = new class extends Exception implements ClientAware {
37+
public function isClientSafe()
38+
{
39+
return true;
40+
}
41+
42+
public function getCategory()
43+
{
44+
return 'foo';
45+
}
46+
};
47+
$clientAwareError = new Error('Foo', null, null, null, null, $clientAwareException);
48+
3549
$executionResult = new ExecutionResult(null, [ $errorCode0 ]);
3650
$this->assertSame(500, $codeDecider->decideHttpStatusCode($executionResult));
3751

@@ -40,5 +54,8 @@ public function testDecideHttpStatusCode(): void
4054

4155
$executionResult = new ExecutionResult(null, [ $graphqlError ]);
4256
$this->assertSame(400, $codeDecider->decideHttpStatusCode($executionResult));
57+
58+
$executionResult = new ExecutionResult(null, [ $clientAwareError ]);
59+
$this->assertSame(400, $codeDecider->decideHttpStatusCode($executionResult));
4360
}
4461
}

0 commit comments

Comments
 (0)