Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ sentry:
client:
integrations:
- Contributte\Sentry\Integration\IgnoreErrorIntegration([
ignore_exception_instance: [
FooException::class,
BarException::class,
],
ignore_exception_regex: [
'/Deprecated (.*)/'
],
Expand Down
25 changes: 25 additions & 0 deletions src/Integration/IgnoreErrorIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ public function __construct(array $options = [])
{
$resolver = new OptionsResolver();
$resolver->setDefaults([
'ignore_exception_instance' => [],
'ignore_exception_regex' => [],
'ignore_message_regex' => [],
]);

$resolver->setAllowedTypes('ignore_exception_instance', ['array']);
$resolver->setAllowedTypes('ignore_exception_regex', ['array']);
$resolver->setAllowedTypes('ignore_message_regex', ['array']);

Expand All @@ -33,6 +35,10 @@ public function __construct(array $options = [])

public function setup(HubInterface $hub, Event $event, EventHint $hint): ?Event
{
if ($this->isIgnoredByExceptionInstance($event)) {
return null;
}

if ($this->isIgnoredByExceptionRegex($event)) {
return null;
}
Expand All @@ -44,6 +50,25 @@ public function setup(HubInterface $hub, Event $event, EventHint $hint): ?Event
return $event;
}

protected function isIgnoredByExceptionInstance(Event $event): bool
{
$exceptions = $event->getExceptions();

if ($exceptions === []) {
return false;
}

/** @var string[] $instances */
$instances = $this->options['ignore_exception_instance'];
foreach ($instances as $instance) {
if ($exceptions[0]->getType() === $instance) {
return true;
}
}

return false;
}

protected function isIgnoredByExceptionRegex(Event $event): bool
{
$exceptions = $event->getExceptions();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types = 1);

use Contributte\Sentry\Integration\IgnoreErrorIntegration;
use Contributte\Tester\Toolkit;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\ExceptionDataBag;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Tester\Assert;
use function Sentry\withScope;

require_once __DIR__ . '/../../../bootstrap.php';

class GoodException extends RuntimeException

Check failure on line 15 in tests/Cases/Integration/IgnoreErrorIntegration/IgnoreMessageWithInstance.phpt

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.3)

Class name GoodException does not match filepath /home/runner/work/sentry/sentry/tests/Cases/Integration/IgnoreErrorIntegration/IgnoreMessageWithInstance.phpt.

Check failure on line 15 in tests/Cases/Integration/IgnoreErrorIntegration/IgnoreMessageWithInstance.phpt

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.3)

Filename doesn't match class name; expected file name "GoodException.phpt"

Check failure on line 15 in tests/Cases/Integration/IgnoreErrorIntegration/IgnoreMessageWithInstance.phpt

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.3)

Each class must be in a namespace of at least one level (a top-level vendor name)
{

Check failure on line 16 in tests/Cases/Integration/IgnoreErrorIntegration/IgnoreMessageWithInstance.phpt

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.3)

There must be one empty line after class opening brace.
}

Check failure on line 17 in tests/Cases/Integration/IgnoreErrorIntegration/IgnoreMessageWithInstance.phpt

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.3)

There must be one empty line before class closing brace.

class BadException extends RuntimeException

Check failure on line 19 in tests/Cases/Integration/IgnoreErrorIntegration/IgnoreMessageWithInstance.phpt

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.3)

Class name BadException does not match filepath /home/runner/work/sentry/sentry/tests/Cases/Integration/IgnoreErrorIntegration/IgnoreMessageWithInstance.phpt.

Check failure on line 19 in tests/Cases/Integration/IgnoreErrorIntegration/IgnoreMessageWithInstance.phpt

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.3)

Filename doesn't match class name; expected file name "BadException.phpt"

Check failure on line 19 in tests/Cases/Integration/IgnoreErrorIntegration/IgnoreMessageWithInstance.phpt

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.3)

Each class must be in a namespace of at least one level (a top-level vendor name)

Check failure on line 19 in tests/Cases/Integration/IgnoreErrorIntegration/IgnoreMessageWithInstance.phpt

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.3)

Each class must be in a file by itself
{

Check failure on line 20 in tests/Cases/Integration/IgnoreErrorIntegration/IgnoreMessageWithInstance.phpt

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.3)

There must be one empty line after class opening brace.
}

// not matched exception and matched event
Toolkit::test(function (): void {
$integration = new IgnoreErrorIntegration([
'ignore_exception_instance' => [
GoodException::class,
],
]);
$integration->setupOnce();

$client = Mockery::mock(ClientInterface::class);
$client->shouldReceive('getIntegration')
->once()
->andReturn($integration);

SentrySdk::getCurrentHub()->bindClient($client);

$event = Event::createEvent();
$event->setMessage('foo bar');
$event->setExceptions([new ExceptionDataBag(new GoodException('bar foo'))]);

withScope(function (Scope $scope) use ($event): void {
$event = $scope->applyToEvent($event);
Assert::null($event);
});
});

Toolkit::test(function (): void {
$integration = new IgnoreErrorIntegration([
'ignore_exception_instance' => [
GoodException::class,
],
]);
$integration->setupOnce();

$client = Mockery::mock(ClientInterface::class);
$client->shouldReceive('getIntegration')
->once()
->andReturn($integration);

SentrySdk::getCurrentHub()->bindClient($client);

$event = Event::createEvent();
$event->setMessage('foo bar');
$event->setExceptions([new ExceptionDataBag(new BadException('bar foo'))]);

withScope(function (Scope $scope) use ($event): void {
$event = $scope->applyToEvent($event);
Assert::notNull($event);
});
});
Loading