Skip to content

Commit 7610599

Browse files
voskobovichrccrdpccl
authored andcommitted
Improved processing the server response with errors (#14)
* Fixed throwing exception on invalid request * Refactored exception message
1 parent 2a25cf4 commit 7610599

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/ResponseBuilder.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ResponseBuilder
99
public function build(ResponseInterface $httpResponse)
1010
{
1111
$body = $httpResponse->getBody();
12-
12+
1313
$normalizedResponse = $this->getNormalizedResponse($body);
1414

1515
return new Response($normalizedResponse['data'], $normalizedResponse['errors']);
@@ -18,9 +18,11 @@ public function build(ResponseInterface $httpResponse)
1818
private function getNormalizedResponse(string $body)
1919
{
2020
$decodedResponse = $this->getJsonDecodedResponse($body);
21-
22-
if (!array_key_exists('data', $decodedResponse)) {
23-
throw new \UnexpectedValueException('Invalid GraphQL JSON response.');
21+
22+
if (false === array_key_exists('data', $decodedResponse) && empty($decodedResponse['errors'])) {
23+
throw new \UnexpectedValueException(
24+
'Invalid GraphQL JSON response. Response body: ' . json_encode($decodedResponse)
25+
);
2426
}
2527

2628
return [
@@ -35,8 +37,11 @@ private function getJsonDecodedResponse(string $body)
3537

3638
$error = json_last_error();
3739
if (JSON_ERROR_NONE !== $error) {
38-
throw new \UnexpectedValueException('Invalid JSON response.');
40+
throw new \UnexpectedValueException(
41+
'Invalid JSON response. Response body: ' . $body
42+
);
3943
}
44+
4045
return $response;
4146
}
4247
}

tests/ResponseBuilderTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function testBuildMalformedResponse()
1515
->willReturn('malformed response');
1616

1717
$this->expectException(\UnexpectedValueException::class);
18-
$this->expectExceptionMessage('Invalid JSON response.');
18+
$this->expectExceptionMessage('Invalid JSON response. Response body: ');
1919

2020
$builder = new ResponseBuilder();
2121
$builder->build($mockHttpResponse);
@@ -45,7 +45,7 @@ public function testBuildInvalidGraphqlJsonResponse(string $body)
4545
->willReturn($body);
4646

4747
$this->expectException(\UnexpectedValueException::class);
48-
$this->expectExceptionMessage('Invalid GraphQL JSON response.');
48+
$this->expectExceptionMessage('Invalid GraphQL JSON response. Response body: ');
4949

5050
$builder = new ResponseBuilder();
5151
$builder->build($mockHttpResponse);
@@ -68,13 +68,28 @@ public function testBuildValidGraphqlJsonWithoutErrors()
6868
);
6969
}
7070

71-
public function testBuildValidGraphqlJsonWithErrors()
71+
public function buildValidGraphqlJsonWithErrorsProvider()
72+
{
73+
return [
74+
'Response with null data' => [
75+
'body' => '{"data": null, "errors": [{"foo": "bar"}]}',
76+
],
77+
'Response without data' => [
78+
'body' => '{"errors": [{"foo": "bar"}]}',
79+
],
80+
];
81+
}
82+
83+
/**
84+
* @dataProvider buildValidGraphqlJsonWithErrorsProvider
85+
*/
86+
public function testBuildValidGraphqlJsonWithErrors(string $body)
7287
{
7388
$mockHttpResponse = $this->createMock(\Psr\Http\Message\ResponseInterface::class);
7489

7590
$mockHttpResponse->expects($this->once())
7691
->method('getBody')
77-
->willReturn('{"data": null, "errors": [{"foo": "bar"}]}');
92+
->willReturn($body);
7893

7994
$builder = new ResponseBuilder();
8095
$response = $builder->build($mockHttpResponse);

0 commit comments

Comments
 (0)