Skip to content

Commit 8647f8f

Browse files
emololftwf3l1x
authored andcommitted
feat: ignore errors by exception instance
1 parent 482b94d commit 8647f8f

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

.docs/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ sentry:
195195
client:
196196
integrations:
197197
- Contributte\Sentry\Integration\IgnoreErrorIntegration([
198+
ignore_exception_instance: [
199+
FooException::class,
200+
BarException::class,
201+
],
198202
ignore_exception_regex: [
199203
'/Deprecated (.*)/'
200204
],

src/Integration/IgnoreErrorIntegration.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ public function __construct(array $options = [])
2121
{
2222
$resolver = new OptionsResolver();
2323
$resolver->setDefaults([
24+
'ignore_exception_instance' => [],
2425
'ignore_exception_regex' => [],
2526
'ignore_message_regex' => [],
2627
]);
2728

29+
$resolver->setAllowedTypes('ignore_exception_instance', ['array']);
2830
$resolver->setAllowedTypes('ignore_exception_regex', ['array']);
2931
$resolver->setAllowedTypes('ignore_message_regex', ['array']);
3032

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

3436
public function setup(HubInterface $hub, Event $event, EventHint $hint): ?Event
3537
{
38+
if ($this->isIgnoredByExceptionInstance($event)) {
39+
return null;
40+
}
41+
3642
if ($this->isIgnoredByExceptionRegex($event)) {
3743
return null;
3844
}
@@ -44,6 +50,25 @@ public function setup(HubInterface $hub, Event $event, EventHint $hint): ?Event
4450
return $event;
4551
}
4652

53+
protected function isIgnoredByExceptionInstance(Event $event): bool
54+
{
55+
$exceptions = $event->getExceptions();
56+
57+
if ($exceptions === []) {
58+
return false;
59+
}
60+
61+
/** @var string[] $instances */
62+
$instances = $this->options['ignore_exception_instance'];
63+
foreach ($instances as $instance) {
64+
if ($exceptions[0]->getType() === $instance) {
65+
return true;
66+
}
67+
}
68+
69+
return false;
70+
}
71+
4772
protected function isIgnoredByExceptionRegex(Event $event): bool
4873
{
4974
$exceptions = $event->getExceptions();
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php declare(strict_types = 1);
2+
3+
use Contributte\Sentry\Integration\IgnoreErrorIntegration;
4+
use Contributte\Tester\Toolkit;
5+
use Sentry\ClientInterface;
6+
use Sentry\Event;
7+
use Sentry\ExceptionDataBag;
8+
use Sentry\SentrySdk;
9+
use Sentry\State\Scope;
10+
use Tester\Assert;
11+
use function Sentry\withScope;
12+
13+
require_once __DIR__ . '/../../../bootstrap.php';
14+
15+
class GoodException extends RuntimeException
16+
{
17+
}
18+
19+
class BadException extends RuntimeException
20+
{
21+
}
22+
23+
// not matched exception and matched event
24+
Toolkit::test(function (): void {
25+
$integration = new IgnoreErrorIntegration([
26+
'ignore_exception_instance' => [
27+
GoodException::class,
28+
],
29+
]);
30+
$integration->setupOnce();
31+
32+
$client = Mockery::mock(ClientInterface::class);
33+
$client->shouldReceive('getIntegration')
34+
->once()
35+
->andReturn($integration);
36+
37+
SentrySdk::getCurrentHub()->bindClient($client);
38+
39+
$event = Event::createEvent();
40+
$event->setMessage('foo bar');
41+
$event->setExceptions([new ExceptionDataBag(new GoodException('bar foo'))]);
42+
43+
withScope(function (Scope $scope) use ($event): void {
44+
$event = $scope->applyToEvent($event);
45+
Assert::null($event);
46+
});
47+
});
48+
49+
Toolkit::test(function (): void {
50+
$integration = new IgnoreErrorIntegration([
51+
'ignore_exception_instance' => [
52+
GoodException::class,
53+
],
54+
]);
55+
$integration->setupOnce();
56+
57+
$client = Mockery::mock(ClientInterface::class);
58+
$client->shouldReceive('getIntegration')
59+
->once()
60+
->andReturn($integration);
61+
62+
SentrySdk::getCurrentHub()->bindClient($client);
63+
64+
$event = Event::createEvent();
65+
$event->setMessage('foo bar');
66+
$event->setExceptions([new ExceptionDataBag(new BadException('bar foo'))]);
67+
68+
withScope(function (Scope $scope) use ($event): void {
69+
$event = $scope->applyToEvent($event);
70+
Assert::notNull($event);
71+
});
72+
});

0 commit comments

Comments
 (0)