From b0823ec5d6aee85b8575ecf4d32684c0dcc9a30c Mon Sep 17 00:00:00 2001 From: Pangratios Cosma Date: Tue, 7 Jan 2025 10:21:00 +0200 Subject: [PATCH] fix(checkins): read error message from response body --- src/Exceptions/ServiceException.php | 9 +++++++++ src/Exceptions/ServiceExceptionFactory.php | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Exceptions/ServiceException.php b/src/Exceptions/ServiceException.php index 606eb08..6f04292 100644 --- a/src/Exceptions/ServiceException.php +++ b/src/Exceptions/ServiceException.php @@ -85,4 +85,13 @@ public static function missingPersonalAuthToken(): self { return new static("Missing personal auth token. This token is required to use Honeybadger's Data APIs."); } + + /** + * @param string $message + * @return self + */ + public static function withMessage(string $message): self + { + return new static($message); + } } diff --git a/src/Exceptions/ServiceExceptionFactory.php b/src/Exceptions/ServiceExceptionFactory.php index 0923309..5c593f3 100644 --- a/src/Exceptions/ServiceExceptionFactory.php +++ b/src/Exceptions/ServiceExceptionFactory.php @@ -27,6 +27,14 @@ public function make(bool $isFromEventsApi = false): ServiceException private function exception(bool $isFromEventsApi = false): ServiceException { + $message = $this->response->getBody()->getContents(); + if (!empty($message)) { + $data = json_decode($message, true); + if (isset($data['errors'])) { + return ServiceException::withMessage($data['errors']); + } + } + if ($this->response->getStatusCode() === Response::HTTP_FORBIDDEN) { return ServiceException::invalidApiKey(); }