From 32d5714692c28982b8b785554173f5005ef40546 Mon Sep 17 00:00:00 2001 From: jee7 Date: Tue, 13 Oct 2020 04:05:44 +0300 Subject: [PATCH] Propagate the Discord API response exceptions (#42) --- src/Discord.php | 4 ++-- src/Exceptions/CouldNotSendNotification.php | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Discord.php b/src/Discord.php index fe74078..6f1438f 100644 --- a/src/Discord.php +++ b/src/Discord.php @@ -90,7 +90,7 @@ protected function request($verb, $endpoint, array $data) ]); } catch (RequestException $exception) { if ($response = $exception->getResponse()) { - throw CouldNotSendNotification::serviceRespondedWithAnHttpError($response); + throw CouldNotSendNotification::serviceRespondedWithAnHttpError($response, $response->getStatusCode(), $exception); } throw CouldNotSendNotification::serviceCommunicationError($exception); @@ -101,7 +101,7 @@ protected function request($verb, $endpoint, array $data) $body = json_decode($response->getBody(), true); if (Arr::get($body, 'code', 0) > 0) { - throw CouldNotSendNotification::serviceRespondedWithAnApiError($body); + throw CouldNotSendNotification::serviceRespondedWithAnApiError($body, $body['code']); } return $body; diff --git a/src/Exceptions/CouldNotSendNotification.php b/src/Exceptions/CouldNotSendNotification.php index 731b425..409417d 100644 --- a/src/Exceptions/CouldNotSendNotification.php +++ b/src/Exceptions/CouldNotSendNotification.php @@ -10,10 +10,12 @@ class CouldNotSendNotification extends Exception { /** * @param \Psr\Http\Message\ResponseInterface $response + * @param int $code + * @param \Exception $exception * * @return static */ - public static function serviceRespondedWithAnHttpError(ResponseInterface $response) + public static function serviceRespondedWithAnHttpError(ResponseInterface $response, $code, $exception) { $message = "Discord responded with an HTTP error: {$response->getStatusCode()}"; @@ -21,17 +23,18 @@ public static function serviceRespondedWithAnHttpError(ResponseInterface $respon $message .= ": $error"; } - return new static($message); + return new static($message, $code, $exception); } /** * @param array $response + * @param int $code * * @return static */ - public static function serviceRespondedWithAnApiError(array $response) + public static function serviceRespondedWithAnApiError(array $response, $code, $exception) { - return new static("Discord responded with an API error: {$response['code']}: {$response['message']}"); + return new static("Discord responded with an API error: {$response['code']}: {$response['message']}", $code); } /** @@ -41,6 +44,6 @@ public static function serviceRespondedWithAnApiError(array $response) */ public static function serviceCommunicationError(Exception $exception) { - return new static("Communication with Discord failed: {$exception->getCode()}: {$exception->getMessage()}"); + return new static("Communication with Discord failed: {$exception->getCode()}: {$exception->getMessage()}", $exception->getCode(), $exception); } }