From 5b87b8b191309b0aaef0a29466b322b5bc43bfab Mon Sep 17 00:00:00 2001 From: Alies Lapatsin Date: Sat, 24 Feb 2024 15:39:50 +0530 Subject: [PATCH 1/2] Better error messages for ip-api.com --- src/Location.php | 5 ++--- src/Services/IPApi.php | 14 ++++++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) 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..1c2ba2c 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([ From 2d6d90d817ac442b054efaee922ee5d8e0875faf Mon Sep 17 00:00:00 2001 From: alies-dev Date: Sat, 24 Feb 2024 10:10:10 +0000 Subject: [PATCH 2/2] Fix coding style --- src/Services/IPApi.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Services/IPApi.php b/src/Services/IPApi.php index 1c2ba2c..a7b8ef6 100644 --- a/src/Services/IPApi.php +++ b/src/Services/IPApi.php @@ -73,12 +73,12 @@ public function locate($ip) // 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"); + throw new \RuntimeException("Unexpected ip-api.com response: {$json->message}"); } // Verify response status if ($json->status !== 'success') { - throw new \RuntimeException("Failed ip-api.com response: $json->message"); + throw new \RuntimeException("Failed ip-api.com response: {$json->message}"); } return $this->hydrate([