Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applied status specific exceptions and updated docs #56

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -199,6 +200,21 @@ You can pass `false` to disable Verification of SSL Certification.

Or You can Pass the path to the certificate.

<a name=exceptions></a>
# 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.
Expand Down
28 changes: 27 additions & 1 deletion src/Exceptions/GooglePlacesApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions src/Exceptions/InvalidRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
namespace SKAgarwal\GoogleApi\Exceptions;

class InvalidRequestException extends GooglePlacesApiException{}
4 changes: 4 additions & 0 deletions src/Exceptions/NotImplementedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
namespace SKAgarwal\GoogleApi\Exceptions;

class NotImplementedException extends GooglePlacesApiException{}
4 changes: 4 additions & 0 deletions src/Exceptions/OverQueryLimitException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
namespace SKAgarwal\GoogleApi\Exceptions;

class OverQueryLimitException extends GooglePlacesApiException{}
4 changes: 4 additions & 0 deletions src/Exceptions/RequestDeniedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
namespace SKAgarwal\GoogleApi\Exceptions;

class RequestDeniedException extends GooglePlacesApiException{}
4 changes: 4 additions & 0 deletions src/Exceptions/UnknownErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
namespace SKAgarwal\GoogleApi\Exceptions;

class UnknownErrorException extends GooglePlacesApiException{}
43 changes: 34 additions & 9 deletions src/PlacesApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
use GuzzleHttp\Client;
use GuzzleHttp\TransferStats;
use SKAgarwal\GoogleApi\Exceptions\GooglePlacesApiException;
use SKAgarwal\GoogleApi\Exceptions\InvalidRequestException;
use SKAgarwal\GoogleApi\Exceptions\NotImplementedException;
use SKAgarwal\GoogleApi\Exceptions\OverQueryLimitException;
use SKAgarwal\GoogleApi\Exceptions\RequestDeniedException;
use SKAgarwal\GoogleApi\Exceptions\UnknownErrorException;

class PlacesApi
{
Expand Down Expand Up @@ -243,16 +248,36 @@ private function makeRequest($uri, $params, $method = 'get')

$this->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;
}

/**
Expand Down