diff --git a/README.md b/README.md index 0be4fdb..9c679df 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + [![Latest Stable Version](https://poser.pugx.org/skagarwal/google-places-api/v/stable?format=flat-square)](https://packagist.org/packages/skagarwal/google-places-api) [![Latest Unstable Version](https://poser.pugx.org/skagarwal/google-places-api/v/unstable?format=flat-square)](https://packagist.org/packages/skagarwal/google-places-api) [![Total Downloads](https://poser.pugx.org/skagarwal/google-places-api/downloads?format=flat-square)](https://packagist.org/packages/skagarwal/google-places-api) @@ -199,6 +200,21 @@ You can pass `false` to disable Verification of SSL Certification. Or You can Pass the path to the certificate. + +# Exceptions +Google Places API may throw various exceptions based on the given `$params` or response and is located in the `SKAgarwal\GoogleApi\Exceptions` namespace. + +- A `GooglePlacesApiException` is thrown when no `API KEY` is provided or `$params` is invalid. +**Note:** This is the parent class for the preceding exceptions. +- A `InvalidRequestException` is thrown when the response `status` is `INVALID_REQUEST` +- A `OverQueryLimitException` is thrown when the response `status` is `OVER_QUERY_LIMIT` +- A `RequestDeniedException` is thrown when the response `status` is `REQUEST_DENIED` +- A `UnknownErrorException` is thrown when the response `status` is `UNKNOWN_ERROR` +- A `NotImplementedException` is thrown when the response cannot be determined. + +If any of these exception has been thrown, you can use the `getErrorMessage()` method to get the `error_message` field from the response if any is provided. +**Note:** `error_message` field is not guaranteed to be always present, and its content is subject to change. + # Contribution Feel free to report issues or make Pull Requests. If you find this document can be improved in any way, please feel free to open an issue for it. diff --git a/src/Exceptions/GooglePlacesApiException.php b/src/Exceptions/GooglePlacesApiException.php index cf9892e..dfa962c 100644 --- a/src/Exceptions/GooglePlacesApiException.php +++ b/src/Exceptions/GooglePlacesApiException.php @@ -3,5 +3,31 @@ class GooglePlacesApiException extends \Exception { - + /** + * @var mixed + */ + private $error_message = null; + + /** + * GooglePlacesApiException constructor. + * + * @param string $message + * @param mixed $error_message + */ + public function __construct ($message = "", $error_message = null ) + { + parent::__construct($message); + + $this->error_message = $error_message; + } + + /** + * Get the error message + * + * @return mixed + */ + public function getErrorMessage() + { + return $this->error_message; + } } diff --git a/src/Exceptions/InvalidRequestException.php b/src/Exceptions/InvalidRequestException.php new file mode 100644 index 0000000..e21e52b --- /dev/null +++ b/src/Exceptions/InvalidRequestException.php @@ -0,0 +1,4 @@ +setStatus($response['status']); - if ($response['status'] !== 'OK' - AND $response['status'] !== 'ZERO_RESULTS') { - throw new GooglePlacesApiException( - "Response returned with status: " . $response['status'] . "\n" . - array_key_exists('error_message', $response) - ?: "Error Message: {$response['error_message']}" - ); + switch($response['status']){ + case 'OK': + case 'ZERO_RESULTS': + return $response; + case 'INVALID_REQUEST': + throw new InvalidRequestException( + "Response returned with status: " . $response['status'], + $response['error_message'] ?? null + ); + case 'OVER_QUERY_LIMIT': + throw new OverQueryLimitException( + "Response returned with status: " . $response['status'], + $response['error_message'] ?? null + ); + case 'REQUEST_DENIED': + throw new RequestDeniedException( + "Response returned with status: " . $response['status'], + $response['error_message'] ?? null + ); + case 'UNKNOWN_ERROR': + throw new UnknownErrorException( + "Response returned with status: " . $response['status'], + $response['error_message'] ?? null + ); + default: + throw new NotImplementedException( + "Response returned with status: " . $response['status'], + $response['error_message'] ?? null + ); } - - return $response; } /**