diff --git a/src/Common/Exception/InvalidServerResponse.php b/src/Common/Exception/InvalidServerResponse.php index 09734a2a3..2c2dcf1a0 100644 --- a/src/Common/Exception/InvalidServerResponse.php +++ b/src/Common/Exception/InvalidServerResponse.php @@ -17,8 +17,24 @@ */ class InvalidServerResponse extends \RuntimeException implements Exception { - public static function create($query) + /** + * @param string $query + * @param int $code + * + * @return InvalidServerResponse + */ + public static function create($query, $code = 0) { - return new self(sprintf('The geocoder server returned an invalid response for query "%s". We could not parse it.', $query)); + return new self(sprintf('The geocoder server returned an invalid response (%d) for query "%s". We could not parse it.', $code, $query)); + } + + /** + * @param string $query + * + * @return InvalidServerResponse + */ + public static function emptyResponse($query) + { + return new self(sprintf('The geocoder server returned an empty response for query "%s".', $query)); } } diff --git a/src/Common/Provider/AbstractHttpProvider.php b/src/Common/Provider/AbstractHttpProvider.php index 6c8f01941..ab893e554 100644 --- a/src/Common/Provider/AbstractHttpProvider.php +++ b/src/Common/Provider/AbstractHttpProvider.php @@ -10,6 +10,9 @@ namespace Geocoder\Provider; +use Geocoder\Exception\InvalidCredentials; +use Geocoder\Exception\InvalidServerResponse; +use Geocoder\Exception\QuotaExceeded; use Http\Message\MessageFactory; use Http\Discovery\MessageFactoryDiscovery; use Http\Client\HttpClient; @@ -39,6 +42,35 @@ public function __construct(HttpClient $client, MessageFactory $factory = null) $this->messageFactory = $factory; } + /** + * Get URL and retrun contents. + * + * @param string $url + * + * @return string + */ + protected function getUrlContents($url) + { + $request = $this->getMessageFactory()->createRequest('GET', $url); + $response = $this->getHttpClient()->sendRequest($request); + + $statusCode = $response->getStatusCode(); + if (401 === $statusCode) { + throw new InvalidCredentials(); + } elseif (429 === $statusCode) { + throw new QuotaExceeded(); + } elseif ($statusCode >= 300) { + throw InvalidServerResponse::create($url, $statusCode); + } + + $body = (string) $response->getBody(); + if (empty($body)) { + throw InvalidServerResponse::emptyResponse($url); + } + + return $body; + } + /** * Returns the HTTP adapter. * diff --git a/src/Provider/GoogleMaps/GoogleMaps.php b/src/Provider/GoogleMaps/GoogleMaps.php index 54602315e..85aa2a174 100644 --- a/src/Provider/GoogleMaps/GoogleMaps.php +++ b/src/Provider/GoogleMaps/GoogleMaps.php @@ -179,18 +179,13 @@ private function buildQuery($url, $locale) private function fetchUrl($url, $locale, $limit) { $url = $this->buildQuery($url, $locale); - $request = $this->getMessageFactory()->createRequest('GET', $url); - $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); + $content = $this->getUrlContents($url); // Throw exception if invalid clientID and/or privateKey used with GoogleMapsBusinessProvider if (strpos($content, "Provided 'signature' is not valid for the provided client ID") !== false) { throw new InvalidCredentials(sprintf('Invalid client ID / API Key %s', $url)); } - if (empty($content)) { - throw InvalidServerResponse::create($url); - } - $json = json_decode($content); // API error