forked from contributte/sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIgnoreErrorIntegration.php
More file actions
108 lines (85 loc) · 2.28 KB
/
IgnoreErrorIntegration.php
File metadata and controls
108 lines (85 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php declare(strict_types = 1);
namespace Contributte\Sentry\Integration;
use Contributte\Sentry\Utils\Regex;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\State\HubInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class IgnoreErrorIntegration extends BaseIntegration
{
/** @var mixed[] */
private array $options;
/**
* @param mixed[] $options
*/
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']);
$this->options = $resolver->resolve($options);
}
public function setup(HubInterface $hub, Event $event, EventHint $hint): ?Event
{
if ($this->isIgnoredByExceptionInstance($event)) {
return null;
}
if ($this->isIgnoredByExceptionRegex($event)) {
return null;
}
if ($this->isIgnoredByMessageRegex($event)) {
return null;
}
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();
if ($exceptions === []) {
return false;
}
/** @var string[] $regexes */
$regexes = $this->options['ignore_exception_regex'];
foreach ($regexes as $regex) {
if (Regex::match($exceptions[0]->getValue(), $regex) !== null) {
return true;
}
}
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;
}
}