Skip to content
This repository has been archived by the owner on Nov 15, 2018. It is now read-only.

Commit

Permalink
Throw API key invalid exception
Browse files Browse the repository at this point in the history
  • Loading branch information
dragosprotung committed Jun 3, 2016
1 parent 7d318eb commit 636af9e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\Exception\ClientException;
use JMS\Serializer\SerializerInterface;
use Speicher210\KontaktIO\Exception\ApiException;
use Speicher210\KontaktIO\Exception\ApiKeyInvalidException;
use Speicher210\KontaktIO\Model\ApiErrorResponse;

/**
Expand Down Expand Up @@ -47,6 +48,11 @@ public function __construct(Client $client, SerializerInterface $serializer)
protected function createApiException(ClientException $e)
{
$response = $e->getResponse();

if ($response->getStatusCode() === 401 || $response->getStatusCode() === 403) {
throw new ApiKeyInvalidException($response);
}

if ($response->getBody()->getSize() > 0) {
/** @var ApiErrorResponse $apiErrorResponse */
$apiErrorResponse = $this->serializer->deserialize(
Expand Down
26 changes: 26 additions & 0 deletions src/Exception/ApiKeyInvalidException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Speicher210\KontaktIO\Exception;

use Psr\Http\Message\ResponseInterface;
use Speicher210\KontaktIO\Model\ApiErrorResponse;

/**
* Exception thrown whe the API key is not valid or not having enough permissions.
*/
class ApiKeyInvalidException extends ApiException
{
/**
* Constructor.
*
* @param ResponseInterface $response The API response.
*/
public function __construct(ResponseInterface $response)
{
$apiErrorResponse = new ApiErrorResponse();
$apiErrorResponse->setStatus($response->getStatusCode());
$apiErrorResponse->setMessage($response->getReasonPhrase());

parent::__construct($apiErrorResponse);
}
}

0 comments on commit 636af9e

Please sign in to comment.