Added specific exceptions for the different types of errors.
This commit is contained in:
parent
3ba84992c8
commit
044ca4d085
10
src/GeoIp2/Exception/AuthenticationException.php
Normal file
10
src/GeoIp2/Exception/AuthenticationException.php
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace GeoIp2\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents a generic error.
|
||||||
|
*/
|
||||||
|
class AuthenticationException extends GeoIp2Exception
|
||||||
|
{
|
||||||
|
}
|
|
@ -6,7 +6,7 @@ namespace GeoIp2\Exception;
|
||||||
* This class represents an error returned by MaxMind's GeoIP2
|
* This class represents an error returned by MaxMind's GeoIP2
|
||||||
* web service.
|
* web service.
|
||||||
*/
|
*/
|
||||||
class WebServiceException extends HttpException
|
class InvalidRequestException extends HttpException
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The code returned by the MaxMind web service
|
* The code returned by the MaxMind web service
|
10
src/GeoIp2/Exception/OutOfQueriesException.php
Normal file
10
src/GeoIp2/Exception/OutOfQueriesException.php
Normal file
|
@ -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\GeoIp2Exception;
|
||||||
use GeoIp2\Exception\HttpException;
|
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\City;
|
||||||
use GeoIp2\Model\CityIspOrg;
|
use GeoIp2\Model\CityIspOrg;
|
||||||
use GeoIp2\Model\Country;
|
use GeoIp2\Model\Country;
|
||||||
|
@ -273,8 +276,7 @@ class Client
|
||||||
$uri
|
$uri
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
$this->handleWebServiceError(
|
||||||
throw new WebServiceException(
|
|
||||||
$body['error'],
|
$body['error'],
|
||||||
$body['code'],
|
$body['code'],
|
||||||
$status,
|
$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)
|
private function handle5xx($response, $uri)
|
||||||
{
|
{
|
||||||
$status = $response->getStatusCode();
|
$status = $response->getStatusCode();
|
||||||
|
|
|
@ -82,6 +82,54 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||||
null,
|
null,
|
||||||
'text/plain'
|
'text/plain'
|
||||||
),
|
),
|
||||||
|
'1.2.3.13'=> $this->response(
|
||||||
|
'error',
|
||||||
|
404,
|
||||||
|
array(
|
||||||
|
'code' => 'IP_ADDRESS_NOT_FOUND',
|
||||||
|
'error' => 'The address "1.2.3.13" is not in our database.'
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'1.2.3.14'=> $this->response(
|
||||||
|
'error',
|
||||||
|
400,
|
||||||
|
array(
|
||||||
|
'code' => 'IP_ADDRESS_RESERVED',
|
||||||
|
'error' => 'The address "1.2.3.14" is a private address.'
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'1.2.3.15'=> $this->response(
|
||||||
|
'error',
|
||||||
|
401,
|
||||||
|
array(
|
||||||
|
'code' => 'AUTHORIZATION_INVALID',
|
||||||
|
'error' => 'A user ID and license key are required to use this service'
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'1.2.3.16'=> $this->response(
|
||||||
|
'error',
|
||||||
|
401,
|
||||||
|
array(
|
||||||
|
'code' => 'LICENSE_KEY_REQUIRED',
|
||||||
|
'error' => 'A license key is required to use this service'
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'1.2.3.17'=> $this->response(
|
||||||
|
'error',
|
||||||
|
401,
|
||||||
|
array(
|
||||||
|
'code' => 'USER_ID_REQUIRED',
|
||||||
|
'error' => 'A user ID is required to use this service'
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'1.2.3.18'=> $this->response(
|
||||||
|
'error',
|
||||||
|
402,
|
||||||
|
array(
|
||||||
|
'code' => 'OUT_OF_QUERIES',
|
||||||
|
'error' => 'The license key you have provided is out of queries.'
|
||||||
|
)
|
||||||
|
),
|
||||||
);
|
);
|
||||||
return $responses[$ip];
|
return $responses[$ip];
|
||||||
}
|
}
|
||||||
|
@ -207,7 +255,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException GeoIp2\Exception\WebServiceException
|
* @expectedException GeoIp2\Exception\InvalidRequestException
|
||||||
* @expectedExceptionCode 400
|
* @expectedExceptionCode 400
|
||||||
* @expectedExceptionMessage The value "1.2.3" is not a valid ip address
|
* @expectedExceptionMessage The value "1.2.3" is not a valid ip address
|
||||||
*/
|
*/
|
||||||
|
@ -216,7 +264,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||||
$client = $this->client($this->getResponse('1.2.3.6'));
|
$client = $this->client($this->getResponse('1.2.3.6'));
|
||||||
|
|
||||||
$client->country('1.2.3.6');
|
$client->country('1.2.3.6');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -293,6 +340,71 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||||
$client->country('1.2.3.12');
|
$client->country('1.2.3.12');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException GeoIp2\Exception\AddressNotFoundException
|
||||||
|
* @expectedExceptionMessage The address "1.2.3.13" is not in our database.
|
||||||
|
*/
|
||||||
|
public function testAddressNotFoundException()
|
||||||
|
{
|
||||||
|
$client = $this->client($this->getResponse('1.2.3.13'));
|
||||||
|
|
||||||
|
$client->country('1.2.3.13');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException GeoIp2\Exception\AddressNotFoundException
|
||||||
|
* @expectedExceptionMessage The address "1.2.3.14" is a private address.
|
||||||
|
*/
|
||||||
|
public function testAddressReservedException()
|
||||||
|
{
|
||||||
|
$client = $this->client($this->getResponse('1.2.3.14'));
|
||||||
|
|
||||||
|
$client->country('1.2.3.14');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException GeoIp2\Exception\AuthenticationException
|
||||||
|
* @expectedExceptionMessage A user ID and license key are required to use this service
|
||||||
|
*/
|
||||||
|
public function testAuthorizationException()
|
||||||
|
{
|
||||||
|
$client = $this->client($this->getResponse('1.2.3.15'));
|
||||||
|
|
||||||
|
$client->country('1.2.3.15');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException GeoIp2\Exception\AuthenticationException
|
||||||
|
* @expectedExceptionMessage A license key is required to use this service
|
||||||
|
*/
|
||||||
|
public function testMissingLicenseKeyException()
|
||||||
|
{
|
||||||
|
$client = $this->client($this->getResponse('1.2.3.16'));
|
||||||
|
|
||||||
|
$client->country('1.2.3.16');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException GeoIp2\Exception\AuthenticationException
|
||||||
|
* @expectedExceptionMessage A user ID is required to use this service
|
||||||
|
*/
|
||||||
|
public function testMissingUserIdException()
|
||||||
|
{
|
||||||
|
$client = $this->client($this->getResponse('1.2.3.17'));
|
||||||
|
|
||||||
|
$client->country('1.2.3.17');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException GeoIp2\Exception\OutOfQueriesException
|
||||||
|
* @expectedExceptionMessage The license key you have provided is out of queries.
|
||||||
|
*/
|
||||||
|
public function testOutOfQueriesException()
|
||||||
|
{
|
||||||
|
$client = $this->client($this->getResponse('1.2.3.18'));
|
||||||
|
|
||||||
|
$client->country('1.2.3.18');
|
||||||
|
}
|
||||||
public function testParams()
|
public function testParams()
|
||||||
{
|
{
|
||||||
$plugin = new MockPlugin();
|
$plugin = new MockPlugin();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user