Skip to content

Better exceptions for ip-api.com #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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