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

View File

@ -0,0 +1,10 @@
<?php
namespace GeoIp2\Exception;
/**
* This class represents a generic error.
*/
class AuthenticationException extends GeoIp2Exception
{
}

View File

@ -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

View File

@ -0,0 +1,10 @@
<?php
namespace GeoIp2\Exception;
/**
* This class represents a generic error.
*/
class OutOfQueriesException extends GeoIp2Exception
{
}

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();

View File

@ -82,6 +82,54 @@ class ClientTest extends \PHPUnit_Framework_TestCase
null,
'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];
}
@ -207,7 +255,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
/**
* @expectedException GeoIp2\Exception\WebServiceException
* @expectedException GeoIp2\Exception\InvalidRequestException
* @expectedExceptionCode 400
* @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->country('1.2.3.6');
}
/**
@ -293,6 +340,71 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$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()
{
$plugin = new MockPlugin();