Skip to content

Commit

Permalink
Handle API errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hgraca authored and Thijs Reijgersberg committed Jun 18, 2019
1 parent b706c31 commit 4417229
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/KvkClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Werkspot\KvkApi;

use function json_encode;
use Werkspot\KvkApi\Client\Factory\KvkPaginatorFactoryInterface;
use Werkspot\KvkApi\Client\KvkPaginator;
use Werkspot\KvkApi\Client\KvkPaginatorInterface;
use Werkspot\KvkApi\Exception\KvkApiException;
use Werkspot\KvkApi\Http\ClientInterface;
use Werkspot\KvkApi\Http\Endpoint\MapperInterface;
use Werkspot\KvkApi\Http\Search\QueryInterface;
Expand Down Expand Up @@ -55,6 +57,22 @@ public function getPreviousPage(KvkPaginatorInterface $kvkPaginator): KvkPaginat

private function decodeJsonToArray(string $json): array
{
return json_decode($json, true)['data'];
$jsonPayload = json_decode($json, true);

if (!isset($jsonPayload['data']) && !isset($jsonPayload['error'])) {
throw new KvkApiException(
"Unknown payload: \n"
. $json
);
}

if (!isset($jsonPayload['data'])) {
throw new KvkApiException(
$jsonPayload['error']['message'] . ': ' . $jsonPayload['error']['reason'],
$jsonPayload['error']['code']
);
}

return $jsonPayload['data'];
}
}
61 changes: 61 additions & 0 deletions tests/Unit/KvkClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Werkspot\KvkApi\Test;

use function json_encode;
use Mockery;
use Mockery\MockInterface;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -43,6 +44,66 @@ public function getProfile(): void
$client->getProfile($profileQuery);
}

/**
* @test
*/
public function getProfileCanHandleApiErrors(): void
{
$code = 404;
$message = 'NotFound';
$reason = 'No companies found for the given query.';
$profileQuery = new ProfileQuery();
$response = $this->getResponse();
$json = <<<RESPONSE
{
"apiVersion": "2.0",
"meta": {},
"error": {
"code": $code,
"message": "$message",
"reason": "$reason"
}
}
RESPONSE;

$adapter = $this->getAdapter();
$adapter->shouldReceive('getEndpoint')->with(MapperInterface::PROFILE, $profileQuery)->once()->andReturn(
$response
);
$adapter->shouldReceive('getJson')->with($response)->once()->andReturn($json);

$client = new KvkClient($adapter, $this->getProfileResponseFactory());
$this->expectExceptionCode(404);
$this->expectExceptionMessage($message . ': ' . $reason);
$client->getProfile($profileQuery);
}

/**
* @test
*/
public function getProfileCanHandleUnknownPayload(): void
{
$profileQuery = new ProfileQuery();
$response = $this->getResponse();
$json = <<<RESPONSE
{
"apiVersion": "2.0",
"meta": {},
"bladibla": "bladibla"
}
RESPONSE;

$adapter = $this->getAdapter();
$adapter->shouldReceive('getEndpoint')->with(MapperInterface::PROFILE, $profileQuery)->once()->andReturn(
$response
);
$adapter->shouldReceive('getJson')->with($response)->once()->andReturn($json);

$client = new KvkClient($adapter, $this->getProfileResponseFactory());
$this->expectExceptionMessageRegExp('/^Unknown payload.*/');
$client->getProfile($profileQuery);
}

/**
* @test
*/
Expand Down

0 comments on commit 4417229

Please sign in to comment.