Skip to content

Commit

Permalink
Rollback to is_a() to ignore exceptions instead of in_array()
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent Mata committed Sep 13, 2023
1 parent e39ed0e commit cdf83cd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,15 @@ private function applyIgnoreOptions(Event $event): ?Event
}

foreach ($exceptions as $exception) {
if (\in_array($exception->getType(), $this->options->getIgnoreExceptions(), true)) {
$this->logger->info(
'The event will be discarded because it matches an entry in "ignore_exceptions".',
['event' => $event]
);

return null;
foreach ($this->options->getIgnoreExceptions() as $ignoredException) {
if (is_a($exception->getType(), $ignoredException, true)) {
$this->logger->info(
'The event will be discarded because it matches an entry in "ignore_exceptions".',
['event' => $event]
);

return null;
}
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Sentry\Severity;
use Sentry\Stacktrace;
use Sentry\State\Scope;
use Sentry\Tests\Fixtures\code\CustomException;
use Sentry\Transport\TransportFactoryInterface;
use Sentry\Transport\TransportInterface;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
Expand Down Expand Up @@ -601,6 +602,29 @@ public function testProcessEventDiscardsEventWhenIgnoreExceptionsMatches(): void
$client->captureException($exception);
}

public function testProcessEventDiscardsEventWhenParentHierarchyOfIgnoreExceptionsMatches(): void
{
$exception = new CustomException('Some foo error');

/** @var LoggerInterface&MockObject $logger */
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->once())
->method('info')
->with('The event will be discarded because it matches an entry in "ignore_exceptions".', $this->callback(static function (array $context): bool {
return isset($context['event']) && $context['event'] instanceof Event;
}));

$options = [
'ignore_exceptions' => [\RuntimeException::class],
];

$client = ClientBuilder::create($options)
->setLogger($logger)
->getClient();

$client->captureException($exception);
}

public function testProcessEventDiscardsEventWhenIgnoreTransactionsMatches(): void
{
$event = Event::createTransaction();
Expand Down
9 changes: 9 additions & 0 deletions tests/Fixtures/code/CustomException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Sentry\Tests\Fixtures\code;

class CustomException extends \RuntimeException
{
}

0 comments on commit cdf83cd

Please sign in to comment.