Repository: geocoder-php/Geocoder (language: php)

Issue:
[GoogleMapsPlaces] Error message response from API is not displayed
In case you use a bad API key, the error message returned from the API is not displayed.


Discussion / hints:

Fix patch:
diff --git a/src/Provider/GoogleMapsPlaces/GoogleMapsPlaces.php b/src/Provider/GoogleMapsPlaces/GoogleMapsPlaces.php
index 079afa2fc..18b90a234 100644
--- a/src/Provider/GoogleMapsPlaces/GoogleMapsPlaces.php
+++ b/src/Provider/GoogleMapsPlaces/GoogleMapsPlaces.php
@@ -433,12 +433,12 @@ private function validateResponse(string $url, $content): StdClass
             throw new InvalidArgument(sprintf('Invalid Request %s', $url));
         }
 
-        if ('REQUEST_DENIED' === $json->status && 'The provided API key is invalid.' === $json->error_messages) {
+        if ('REQUEST_DENIED' === $json->status && 'The provided API key is invalid.' === $json->error_message) {
             throw new InvalidCredentials(sprintf('API key is invalid %s', $url));
         }
 
         if ('REQUEST_DENIED' === $json->status) {
-            throw new InvalidServerResponse(sprintf('API access denied. Request: %s - Message: %s', $url, $json->error_messages));
+            throw new InvalidServerResponse(sprintf('API access denied. Request: %s - Message: %s', $url, $json->error_message));
         }
 
         if ('OVER_QUERY_LIMIT' === $json->status) {
diff --git a/src/Provider/GoogleMapsPlaces/Tests/.cached_responses/maps.googleapis.com_cadff963f476475981c981d8dcffbdf0ef41a5b0 b/src/Provider/GoogleMapsPlaces/Tests/.cached_responses/maps.googleapis.com_cadff963f476475981c981d8dcffbdf0ef41a5b0
new file mode 100644
index 000000000..9d3b13d34
--- /dev/null
+++ b/src/Provider/GoogleMapsPlaces/Tests/.cached_responses/maps.googleapis.com_cadff963f476475981c981d8dcffbdf0ef41a5b0
@@ -0,0 +1,6 @@
+s:114:"{
+   "candidates" : [],
+   "error_message" : "The provided API key is invalid.",
+   "status" : "REQUEST_DENIED"
+}
+";
\ No newline at end of file
diff --git a/src/Provider/GoogleMapsPlaces/Tests/GoogleMapsPlacesTest.php b/src/Provider/GoogleMapsPlaces/Tests/GoogleMapsPlacesTest.php
index 9f7000eb7..f2f106614 100644
--- a/src/Provider/GoogleMapsPlaces/Tests/GoogleMapsPlacesTest.php
+++ b/src/Provider/GoogleMapsPlaces/Tests/GoogleMapsPlacesTest.php
@@ -13,6 +13,7 @@
 namespace Geocoder\Provider\GoogleMapsPlaces\Tests;
 
 use Geocoder\Exception\InvalidArgument;
+use Geocoder\Exception\InvalidCredentials;
 use Geocoder\Exception\QuotaExceeded;
 use Geocoder\Exception\UnsupportedOperation;
 use Geocoder\IntegrationTest\BaseTestCase;
@@ -308,6 +309,15 @@ public function testReverseGeocodePlaceSearchWithEmptyOpeningHours()
         // sometimes giving: Failed asserting that Object ['openNow' => null, 'periods' => [], 'weekdayText' => []] is null
     }
 
+    public function testInvalidAPIKey()
+    {
+        $this->expectException(InvalidCredentials::class);
+        $this->expectExceptionMessageMatches('/^API key is invalid/');
+
+        $provider = new GoogleMapsPlaces($this->getHttpClient('invalidKey'), 'invalidKey');
+        $provider->geocodeQuery(GeocodeQuery::create('White house'));
+    }
+
     private function getGoogleMapsProvider(): GoogleMapsPlaces
     {
         if (!isset($_SERVER['GOOGLE_GEOCODING_KEY'])) {


Test patch: