Skip to content

Commit 0227002

Browse files
authored
Merge pull request #27 from InteractionDesignFoundation/better-exceptions
Better exceptions for ip-api.com
2 parents c2a2b79 + 2d6d90d commit 0227002

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/Location.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ public function getAttribute($key)
105105

106106
// First we will check for the presence of a mutator for the set operation
107107
// which simply lets the developers tweak the attribute as it is set.
108-
if (method_exists($this, 'get' . Str::studly($key) . 'Attribute')) {
109-
$method = 'get' . Str::studly($key) . 'Attribute';
110-
108+
$method = 'get' . Str::studly($key) . 'Attribute';
109+
if (method_exists($this, $method)) {
111110
return $this->{$method}($value);
112111
}
113112

src/Services/IPApi.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,29 @@ public function boot()
5656
}
5757
}
5858

59-
/** {@inheritDoc} */
59+
/**
60+
* {@inheritDoc}
61+
* @throws \RuntimeException
62+
*/
6063
public function locate($ip)
6164
{
62-
// Get data from client
65+
// Get data from the client
6366
$data = $this->client->get('json/' . $ip);
6467

6568
// Verify server response
6669
if ($this->client->getErrors() !== null) {
67-
throw new Exception('Request failed (' . $this->client->getErrors() . ')');
70+
throw new \RuntimeException("Unexpected ip-api.com response: {$this->client->getErrors()}");
6871
}
6972

7073
// Parse body content
7174
$json = json_decode($data[0]);
75+
if (! is_object($json) || ! property_exists($json, 'status')) {
76+
throw new \RuntimeException("Unexpected ip-api.com response: {$json->message}");
77+
}
7278

7379
// Verify response status
7480
if ($json->status !== 'success') {
75-
throw new Exception('Request failed (' . $json->message . ')');
81+
throw new \RuntimeException("Failed ip-api.com response: {$json->message}");
7682
}
7783

7884
return $this->hydrate([

0 commit comments

Comments
 (0)