Skip to content

Commit

Permalink
feat: add events api exception message (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
subzero10 authored Nov 1, 2024
1 parent 3eb1947 commit e0a0b90
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/Exceptions/ServiceException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
10 changes: 6 additions & 4 deletions src/Exceptions/ServiceExceptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/HoneybadgerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
27 changes: 27 additions & 0 deletions tests/HoneybadgerClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit e0a0b90

Please sign in to comment.