Skip to content

Commit 6068f65

Browse files
committed
Add IPinfoException to catch all exception types.
1 parent 5b360e3 commit 6068f65

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

src/IPinfo.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
require_once(__DIR__.'/cache/Default.php');
66

7-
use GuzzleHttp\Exception\TransferException;
7+
use Exception;
8+
use GuzzleHttp\Exception\GuzzleException;
89
use ipinfo\ipinfo\Details;
10+
use ipinfo\ipinfo\IPinfoException;
911

1012
/**
1113
* Exposes the IPinfo library to client code.
@@ -14,7 +16,7 @@ class IPinfo
1416
{
1517
const API_URL = 'https://ipinfo.io';
1618
const CACHE_MAXSIZE = 4096;
17-
const CACHE_TTL = 60 * 60 * 24;
19+
const CACHE_TTL = 86400; // 24 hours as seconds
1820
const COUNTRIES_FILE_DEFAULT = __DIR__ . '/countries.json';
1921
const REQUEST_TYPE_GET = 'GET';
2022
const STATUS_CODE_QUOTA_EXCEEDED = 429;
@@ -45,6 +47,7 @@ public function __construct($access_token = null, $settings = [])
4547
* Get formatted details for an IP address.
4648
* @param string|null $ip_address IP address to look up.
4749
* @return Details Formatted IPinfo data.
50+
* @throws IPinfoException
4851
*/
4952
public function getDetails($ip_address = null)
5053
{
@@ -79,6 +82,7 @@ public function formatDetailsObject($details = [])
7982
* Get details for a specific IP address.
8083
* @param string $ip_address IP address to query API for.
8184
* @return array IP response data.
85+
* @throws IPinfoException
8286
*/
8387
public function getRequestDetails(string $ip_address)
8488
{
@@ -88,16 +92,22 @@ public function getRequestDetails(string $ip_address)
8892
$url .= "/$ip_address";
8993
}
9094

91-
$response = $this->http_client->request(
92-
self::REQUEST_TYPE_GET,
93-
$url,
94-
$this->buildHeaders()
95-
);
95+
try {
96+
$response = $this->http_client->request(
97+
self::REQUEST_TYPE_GET,
98+
$url,
99+
$this->buildHeaders()
100+
);
101+
} catch (GuzzleException $e) {
102+
throw new IPinfoException($e->getMessage());
103+
} catch (Exception $e) {
104+
throw new IPinfoException($e->getMessage());
105+
}
96106

97107
if ($response->getStatusCode() == self::STATUS_CODE_QUOTA_EXCEEDED) {
98-
throw new Exception('IPinfo request quota exceeded.');
108+
throw new IPinfoException('IPinfo request quota exceeded.');
99109
} elseif ($response->getStatusCode() >= 400) {
100-
throw new Exception('Exception: ' . json_encode([
110+
throw new IPinfoException('Exception: ' . json_encode([
101111
'status' => $response->getStatusCode(),
102112
'reason' => $response->getReasonPhrase(),
103113
]));

src/IPinfoException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace ipinfo\ipinfo;
4+
5+
use Exception;
6+
7+
class IPinfoException extends Exception
8+
{
9+
}

tests/IPinfoTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

33
namespace ipinfo\ipinfo;
4-
use ipinfo\ipinfo\IPinfo;
4+
55
use ipinfo\ipinfo\Details;
6+
use ipinfo\ipinfo\IPinfo;
7+
use ipinfo\ipinfo\IPinfoException;
68

79
class IPinfoTest extends \PHPUnit\Framework\TestCase
810
{
@@ -74,4 +76,12 @@ public function testBuildHeaders()
7476
$this->assertEquals("application/json", $headers['accept']);
7577
$this->assertEquals("Bearer $token", $headers['authorization']);
7678
}
79+
80+
public function testBadIP()
81+
{
82+
$ip = "fake_ip";
83+
$handler = new IPinfo();
84+
$this->expectException(IPinfoException::class);
85+
$handler->getDetails($ip);
86+
}
7787
}

0 commit comments

Comments
 (0)