Added specific exceptions for the different types of errors.

This commit is contained in:
Gregory Oschwald
2013-07-16 07:46:50 -07:00
parent 3ba84992c8
commit 044ca4d085
5 changed files with 162 additions and 6 deletions
+27 -3
View File
@@ -4,7 +4,10 @@ namespace GeoIp2\WebService;
use GeoIp2\Exception\GeoIp2Exception;
use GeoIp2\Exception\HttpException;
use GeoIp2\Exception\WebServiceException;
use GeoIp2\Exception\AddressNotFoundException;
use GeoIp2\Exception\AuthenticationException;
use GeoIp2\Exception\InvalidRequestException;
use GeoIp2\Exception\OutOfQueriesException;
use GeoIp2\Model\City;
use GeoIp2\Model\CityIspOrg;
use GeoIp2\Model\Country;
@@ -273,8 +276,7 @@ class Client
$uri
);
}
throw new WebServiceException(
$this->handleWebServiceError(
$body['error'],
$body['code'],
$status,
@@ -282,6 +284,28 @@ class Client
);
}
private function handleWebServiceError($message, $code, $status, $uri)
{
switch ($code) {
case 'IP_ADDRESS_NOT_FOUND':
case 'IP_ADDRESS_RESERVED':
throw new AddressNotFoundException($message);
case 'AUTHORIZATION_INVALID':
case 'LICENSE_KEY_REQUIRED':
case 'USER_ID_REQUIRED':
throw new AuthenticationException($message);
case 'OUT_OF_QUERIES':
throw new OutOfQueriesException($message);
default:
throw new InvalidRequestException(
$message,
$code,
$status,
$uri
);
}
}
private function handle5xx($response, $uri)
{
$status = $response->getStatusCode();