Skip to content

Commit

Permalink
Improved processing the server response with errors (#14)
Browse files Browse the repository at this point in the history
* Fixed throwing exception on invalid request

* Refactored exception message
  • Loading branch information
voskobovich authored and rccrdpccl committed Jun 19, 2018
1 parent 2a25cf4 commit 7610599
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
15 changes: 10 additions & 5 deletions src/ResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ResponseBuilder
public function build(ResponseInterface $httpResponse)
{
$body = $httpResponse->getBody();

$normalizedResponse = $this->getNormalizedResponse($body);

return new Response($normalizedResponse['data'], $normalizedResponse['errors']);
Expand All @@ -18,9 +18,11 @@ public function build(ResponseInterface $httpResponse)
private function getNormalizedResponse(string $body)
{
$decodedResponse = $this->getJsonDecodedResponse($body);

if (!array_key_exists('data', $decodedResponse)) {
throw new \UnexpectedValueException('Invalid GraphQL JSON response.');

if (false === array_key_exists('data', $decodedResponse) && empty($decodedResponse['errors'])) {
throw new \UnexpectedValueException(
'Invalid GraphQL JSON response. Response body: ' . json_encode($decodedResponse)
);
}

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

$error = json_last_error();
if (JSON_ERROR_NONE !== $error) {
throw new \UnexpectedValueException('Invalid JSON response.');
throw new \UnexpectedValueException(
'Invalid JSON response. Response body: ' . $body
);
}

return $response;
}
}
23 changes: 19 additions & 4 deletions tests/ResponseBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function testBuildMalformedResponse()
->willReturn('malformed response');

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

$builder = new ResponseBuilder();
$builder->build($mockHttpResponse);
Expand Down Expand Up @@ -45,7 +45,7 @@ public function testBuildInvalidGraphqlJsonResponse(string $body)
->willReturn($body);

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

$builder = new ResponseBuilder();
$builder->build($mockHttpResponse);
Expand All @@ -68,13 +68,28 @@ public function testBuildValidGraphqlJsonWithoutErrors()
);
}

public function testBuildValidGraphqlJsonWithErrors()
public function buildValidGraphqlJsonWithErrorsProvider()
{
return [
'Response with null data' => [
'body' => '{"data": null, "errors": [{"foo": "bar"}]}',
],
'Response without data' => [
'body' => '{"errors": [{"foo": "bar"}]}',
],
];
}

/**
* @dataProvider buildValidGraphqlJsonWithErrorsProvider
*/
public function testBuildValidGraphqlJsonWithErrors(string $body)
{
$mockHttpResponse = $this->createMock(\Psr\Http\Message\ResponseInterface::class);

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

$builder = new ResponseBuilder();
$response = $builder->build($mockHttpResponse);
Expand Down

0 comments on commit 7610599

Please sign in to comment.