Skip to content

Commit

Permalink
IgnoreErrorIntegration: ignore Event message
Browse files Browse the repository at this point in the history
  • Loading branch information
ojanota authored and f3l1x committed May 24, 2022
1 parent 481cb17 commit 9707fd0
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 3 deletions.
7 changes: 5 additions & 2 deletions .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ sentry:

**IgnoreErrorIntegration**

Allow to ignore exceptions.
Allow to ignore exceptions or event message.

```neon
sentry:
Expand All @@ -191,7 +191,10 @@ sentry:
- Contributte\Sentry\Integration\IgnoreErrorIntegration([
ignore_exception_regex: [
'/Deprecated (.*)/'
]
],
ignore_message_regex: [
'/PHP Deprecated (.*)/'
],
])
```

Expand Down
23 changes: 23 additions & 0 deletions src/Integration/IgnoreErrorIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public function __construct(array $options = [])
$resolver = new OptionsResolver();
$resolver->setDefaults([
'ignore_exception_regex' => [],
'ignore_message_regex' => [],
]);

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

$this->options = $resolver->resolve($options);
}
Expand All @@ -34,6 +36,10 @@ public function setup(HubInterface $hub, Event $event): ?Event
return null;
}

if ($this->isIgnoredByMessageRegex($event)) {
return null;
}

return $event;
}

Expand All @@ -56,4 +62,21 @@ protected function isIgnoredByExceptionRegex(Event $event): bool
return false;
}

protected function isIgnoredByMessageRegex(Event $event): bool
{
if ($event->getMessage() === null) {
return false;
}

/** @var string[] $regexes */
$regexes = $this->options['ignore_message_regex'];
foreach ($regexes as $regex) {
if (Regex::match($event->getMessage(), $regex) !== null) {
return true;
}
}

return false;
}

}
70 changes: 70 additions & 0 deletions tests/Cases/Integration/IgnoreErrorIntegration/FailedIgnore.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php declare(strict_types = 1);

use Contributte\Sentry\Integration\IgnoreErrorIntegration;
use Ninjify\Nunjuck\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';

// not matched exception and event
Toolkit::test(function (): void {
$integration = new IgnoreErrorIntegration([
'ignore_exception_regex' => [
'#foo bar#',
],
'ignore_message_regex' => [
'#foo bar#',
],
]);
$integration->setupOnce();

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

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

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

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

// not matched exception and no event message
Toolkit::test(function (): void {
$integration = new IgnoreErrorIntegration([
'ignore_exception_regex' => [
'#foo bar#',
],
'ignore_message_regex' => [
'#foo bar#',
],
]);
$integration->setupOnce();

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

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

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

withScope(function (Scope $scope) use ($event): void {
$newEvent = $scope->applyToEvent($event);
Assert::equal($event, $newEvent);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use Sentry\State\Scope;
use Tester\Assert;
use function Sentry\withScope;

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

// Ignore exception
Toolkit::test(function (): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

use Contributte\Sentry\Integration\IgnoreErrorIntegration;
use Ninjify\Nunjuck\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';

// Ignore exception and message
Toolkit::test(function (): void {
$integration = new IgnoreErrorIntegration([
'ignore_exception_regex' => [
'#bar foo#',
],
'ignore_message_regex' => [
'#foo bar#',
],
]);
$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 RuntimeException('bar foo'))]);

withScope(function (Scope $scope) use ($event): void {
$event = $scope->applyToEvent($event);
Assert::null($event);
});
});
37 changes: 37 additions & 0 deletions tests/Cases/Integration/IgnoreErrorIntegration/IgnoreMessage.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types = 1);

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

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

// Ignore message
Toolkit::test(function (): void {
$integration = new IgnoreErrorIntegration([
'ignore_message_regex' => [
'#foo bar#',
],
]);
$integration->setupOnce();

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

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

$event = Event::createEvent();
$event->setMessage('foo bar');

withScope(function (Scope $scope) use ($event): void {
$event = $scope->applyToEvent($event);
Assert::null($event);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

use Contributte\Sentry\Integration\IgnoreErrorIntegration;
use Ninjify\Nunjuck\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';

// not matched exception and matched event
Toolkit::test(function (): void {
$integration = new IgnoreErrorIntegration([
'ignore_exception_regex' => [
'#foo bar#',
],
'ignore_message_regex' => [
'#foo bar#',
],
]);
$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 RuntimeException('bar foo'))]);

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

0 comments on commit 9707fd0

Please sign in to comment.