diff --git a/src/Exceptions/MyDataAuthenticationException.php b/src/Exceptions/MyDataAuthenticationException.php index cc73d12..63067c8 100644 --- a/src/Exceptions/MyDataAuthenticationException.php +++ b/src/Exceptions/MyDataAuthenticationException.php @@ -2,10 +2,13 @@ namespace Firebed\AadeMyData\Exceptions; -class MyDataAuthenticationException extends MyDataException + +use Throwable; + +class MyDataAuthenticationException extends TransmissionFailedException { - public function __construct() + public function __construct(int $code = 0, Throwable $previous = null) { - parent::__construct("Authentication failed. Please check your user id and subscription key.", 401); + parent::__construct("Authentication failed. Please check your user id and subscription key.", $code, $previous); } } \ No newline at end of file diff --git a/src/Exceptions/MyDataConnectionException.php b/src/Exceptions/MyDataConnectionException.php index 9fb7a79..1486f41 100644 --- a/src/Exceptions/MyDataConnectionException.php +++ b/src/Exceptions/MyDataConnectionException.php @@ -2,10 +2,12 @@ namespace Firebed\AadeMyData\Exceptions; -class MyDataConnectionException extends MyDataException +use Throwable; + +class MyDataConnectionException extends TransmissionFailedException { - public function __construct() + public function __construct(int $code = 0, Throwable $previous = null) { - parent::__construct("myDATA servers are down or unreachable. Please try again later."); + parent::__construct("myDATA servers are down or unreachable. Please try again later.", $code, $previous); } } \ No newline at end of file diff --git a/src/Exceptions/RateLimitExceededException.php b/src/Exceptions/RateLimitExceededException.php new file mode 100644 index 0000000..ed25b3a --- /dev/null +++ b/src/Exceptions/RateLimitExceededException.php @@ -0,0 +1,13 @@ +client()->get($this->getUrl(), ['query' => $query]); } catch (GuzzleException $e) { - $this->handleException($e); + $this->handleTransmissionException($e); } } @@ -168,25 +170,33 @@ protected function post(array $query = null, string $body = null): ResponseInter try { return $this->client()->post($this->getUrl(), $params); } catch (GuzzleException $e) { - $this->handleException($e); + $this->handleTransmissionException($e); } } /** + * Authorization errors, bad request, communication errors, + * myDATA server errors, rate limits, connection timeout, etc. + * * @throws MyDataAuthenticationException|MyDataException */ - protected function handleException(GuzzleException $exception) + protected function handleTransmissionException(GuzzleException $exception) { if ($exception->getCode() === 401) { - throw new MyDataAuthenticationException(); + throw new MyDataAuthenticationException($exception->getCode(), $exception); } // In case the endpoint url is wrong or connection timed out if ($exception->getCode() === 0) { - throw new MyDataConnectionException(); + throw new MyDataConnectionException($exception->getCode(), $exception); + } + + // Rate limit exception + if ($exception->getCode() === 429) { + throw new RateLimitExceededException($exception->getMessage(), $exception->getCode(), $exception); } - throw new MyDataException($exception->getMessage(), $exception->getCode()); + throw new TransmissionFailedException($exception->getMessage(), $exception->getCode(), $exception); } private function client(): Client