Skip to content

Commit

Permalink
Merge pull request #27 from InteractionDesignFoundation/better-except…
Browse files Browse the repository at this point in the history
…ions

Better exceptions for ip-api.com
  • Loading branch information
alies-dev authored Feb 24, 2024
2 parents c2a2b79 + 2d6d90d commit 0227002
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ public function getAttribute($key)

// First we will check for the presence of a mutator for the set operation
// which simply lets the developers tweak the attribute as it is set.
if (method_exists($this, 'get' . Str::studly($key) . 'Attribute')) {
$method = 'get' . Str::studly($key) . 'Attribute';

$method = 'get' . Str::studly($key) . 'Attribute';
if (method_exists($this, $method)) {
return $this->{$method}($value);
}

Expand Down
14 changes: 10 additions & 4 deletions src/Services/IPApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,29 @@ public function boot()
}
}

/** {@inheritDoc} */
/**
* {@inheritDoc}
* @throws \RuntimeException
*/
public function locate($ip)
{
// Get data from client
// Get data from the client
$data = $this->client->get('json/' . $ip);

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

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

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

return $this->hydrate([
Expand Down

0 comments on commit 0227002

Please sign in to comment.