From cdf83cd52cbe5282b94af6c06d607d7d3527e777 Mon Sep 17 00:00:00 2001 From: Florent Mata Date: Wed, 13 Sep 2023 15:36:09 +0200 Subject: [PATCH] Rollback to is_a() to ignore exceptions instead of in_array() --- src/Client.php | 16 +++++++++------- tests/ClientTest.php | 24 ++++++++++++++++++++++++ tests/Fixtures/code/CustomException.php | 9 +++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 tests/Fixtures/code/CustomException.php diff --git a/src/Client.php b/src/Client.php index 3d0b6d305..448b72772 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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; + } } } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 395c96c82..96b5d50dd 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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; @@ -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(); diff --git a/tests/Fixtures/code/CustomException.php b/tests/Fixtures/code/CustomException.php new file mode 100644 index 000000000..f47296725 --- /dev/null +++ b/tests/Fixtures/code/CustomException.php @@ -0,0 +1,9 @@ +