diff --git a/src/Location.php b/src/Location.php index 04f4c89..3dc07e6 100644 --- a/src/Location.php +++ b/src/Location.php @@ -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); } diff --git a/src/Services/IPApi.php b/src/Services/IPApi.php index 1fa66a5..a7b8ef6 100644 --- a/src/Services/IPApi.php +++ b/src/Services/IPApi.php @@ -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([