Added specific exceptions for the different types of errors.
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Exception;
|
||||
|
||||
/**
|
||||
* This class represents a generic error.
|
||||
*/
|
||||
class AuthenticationException extends GeoIp2Exception
|
||||
{
|
||||
}
|
||||
+1
-1
@@ -6,7 +6,7 @@ namespace GeoIp2\Exception;
|
||||
* This class represents an error returned by MaxMind's GeoIP2
|
||||
* web service.
|
||||
*/
|
||||
class WebServiceException extends HttpException
|
||||
class InvalidRequestException extends HttpException
|
||||
{
|
||||
/**
|
||||
* The code returned by the MaxMind web service
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace GeoIp2\Exception;
|
||||
|
||||
/**
|
||||
* This class represents a generic error.
|
||||
*/
|
||||
class OutOfQueriesException extends GeoIp2Exception
|
||||
{
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user