Skip to content

Commit 50d72d7

Browse files
redthorNyholm
authored andcommitted
Geoip2 provider exceptions (#714)
* Create test case and show handling one exception * Handle credentials and quota exceeded exception transformation, closes #534 * Fix style issues * Fix style issues - spacing
1 parent 29ca799 commit 50d72d7

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/Provider/GeoIP2/GeoIP2.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121
use Geocoder\Provider\IpAddressGeocoder;
2222
use Geocoder\Provider\LocaleAwareGeocoder;
2323
use Geocoder\Provider\Provider;
24-
use GeoIp2\Exception\AddressNotFoundException;
2524
use Geocoder\Exception\UnsupportedOperation;
25+
use Geocoder\Exception\InvalidCredentials;
26+
use Geocoder\Exception\QuotaExceeded;
27+
use GeoIp2\Exception\AddressNotFoundException;
28+
use GeoIp2\Exception\AuthenticationException;
29+
use GeoIp2\Exception\OutOfQueriesException;
2630

2731
/**
2832
* @author Jens Wiese <[email protected]>
@@ -114,6 +118,18 @@ private function executeQuery(string $address): string
114118
$result = $this->adapter->getContent($uri);
115119
} catch (AddressNotFoundException $e) {
116120
return '';
121+
} catch (AuthenticationException $e) {
122+
throw new InvalidCredentials(
123+
$e->getMessage(),
124+
$e->getCode(),
125+
$e
126+
);
127+
} catch (OutOfQueriesException $e) {
128+
throw new QuotaExceeded(
129+
$e->getMessage(),
130+
$e->getCode(),
131+
$e
132+
);
117133
}
118134

119135
return $result;

src/Provider/GeoIP2/Tests/GeoIP2Test.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
use Geocoder\Provider\GeoIP2\GeoIP2;
2121
use Geocoder\Provider\GeoIP2\GeoIP2Adapter;
2222
use GeoIp2\Database\Reader;
23+
use GeoIp2\Exception\AuthenticationException;
24+
use GeoIp2\Exception\OutOfQueriesException;
25+
use Geocoder\Exception\InvalidCredentials;
26+
use Geocoder\Exception\QuotaExceeded;
2327

2428
/**
2529
* @author Jens Wiese <[email protected]>
@@ -223,6 +227,32 @@ public function testGeoIp2Encoding()
223227
$this->assertEquals('Timișoara', $locality);
224228
}
225229

230+
/**
231+
* @dataProvider provideDataForTestingExceptions
232+
*
233+
* @param \Exception $original
234+
* @param string $replacementClass
235+
*/
236+
public function testExceptionConversion(\Exception $original, string $replacementClass)
237+
{
238+
$adapter = $this->getGeoIP2AdapterMock($original);
239+
$provider = new GeoIP2($adapter);
240+
241+
self::expectException($replacementClass);
242+
self::expectExceptionMessage($original->getMessage());
243+
self::expectExceptionCode($original->getCode());
244+
245+
$results = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
246+
}
247+
248+
public static function provideDataForTestingExceptions(): array
249+
{
250+
return [
251+
[new AuthenticationException('Credentials are no good'), InvalidCredentials::class],
252+
[new OutOfQueriesException('You ran out'), QuotaExceeded::class],
253+
];
254+
}
255+
226256
/**
227257
* @param mixed $returnValue
228258
*

0 commit comments

Comments
 (0)