From e0a0b90c6a1acfc188be3cabaf7907d448824bba Mon Sep 17 00:00:00 2001 From: Pangratios Cosma Date: Fri, 1 Nov 2024 18:23:02 +0200 Subject: [PATCH] feat: add events api exception message (#213) --- src/Exceptions/ServiceException.php | 8 +++++++ src/Exceptions/ServiceExceptionFactory.php | 10 ++++---- src/HoneybadgerClient.php | 2 +- tests/HoneybadgerClientTest.php | 27 ++++++++++++++++++++++ 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/Exceptions/ServiceException.php b/src/Exceptions/ServiceException.php index 94fb4a5..77fa9d6 100644 --- a/src/Exceptions/ServiceException.php +++ b/src/Exceptions/ServiceException.php @@ -31,6 +31,14 @@ public static function rateLimit(): self return new static('You have hit your exception rate limit.'); } + /** + * @return ServiceException + */ + public static function eventsRateLimit(): self + { + return new static('You have hit your events rate limit.'); + } + /** * @return ServiceException */ diff --git a/src/Exceptions/ServiceExceptionFactory.php b/src/Exceptions/ServiceExceptionFactory.php index 9e5fb12..0923309 100644 --- a/src/Exceptions/ServiceExceptionFactory.php +++ b/src/Exceptions/ServiceExceptionFactory.php @@ -20,12 +20,12 @@ public function __construct(ResponseInterface $response) $this->response = $response; } - public function make(): ServiceException + public function make(bool $isFromEventsApi = false): ServiceException { - return $this->exception(); + return $this->exception($isFromEventsApi); } - private function exception(): ServiceException + private function exception(bool $isFromEventsApi = false): ServiceException { if ($this->response->getStatusCode() === Response::HTTP_FORBIDDEN) { return ServiceException::invalidApiKey(); @@ -36,7 +36,9 @@ private function exception(): ServiceException } if ($this->response->getStatusCode() === Response::HTTP_TOO_MANY_REQUESTS) { - return ServiceException::rateLimit(); + return $isFromEventsApi + ? ServiceException::eventsRateLimit() + : ServiceException::rateLimit(); } if ($this->response->getStatusCode() === Response::HTTP_INTERNAL_SERVER_ERROR) { diff --git a/src/HoneybadgerClient.php b/src/HoneybadgerClient.php index 1aeebe7..ea957f8 100644 --- a/src/HoneybadgerClient.php +++ b/src/HoneybadgerClient.php @@ -89,7 +89,7 @@ public function events(array $events): void } if ($response->getStatusCode() !== Response::HTTP_CREATED) { - $this->handleEventsException((new ServiceExceptionFactory($response))->make()); + $this->handleEventsException((new ServiceExceptionFactory($response))->make(true)); } } diff --git a/tests/HoneybadgerClientTest.php b/tests/HoneybadgerClientTest.php index 42d0ded..eb75513 100644 --- a/tests/HoneybadgerClientTest.php +++ b/tests/HoneybadgerClientTest.php @@ -9,6 +9,7 @@ use GuzzleHttp\Psr7\Utils; use Honeybadger\Config; use Honeybadger\Exceptions\ServiceException; +use Honeybadger\Exceptions\ServiceExceptionFactory; use Honeybadger\HoneybadgerClient; use Mockery; use PHPUnit\Framework\TestCase; @@ -44,6 +45,32 @@ public function throws_generic_exception_for_checkins() $client->checkIn('1234'); } + /** @test */ + public function throws_rate_limit_exception_for_events() + { + $message = null; + $config = new Config([ + 'api_key' => '1234', + 'events_exception_handler' => function (ServiceException $e) use (&$message) { + $message = $e->getMessage(); + }, + ]); + $mock = Mockery::mock(Client::class)->makePartial(); + $mock->shouldReceive('post')->andThrow(ServiceException::eventsRateLimit()); + + $client = new HoneybadgerClient($config, $mock); + $events = [ + [ + 'event_type' => 'log', + 'ts' => (new DateTime())->format(DATE_RFC3339_EXTENDED), + 'message' => 'Test message' + ] + ]; + $client->events($events); + + $this->assertStringContainsString('You have hit your events rate limit.', $message); + } + /** @test */ public function doesnt_throw_generic_exception_for_events() {