|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sentry\Integration; |
| 6 | + |
| 7 | +use Sentry\Event; |
| 8 | +use Sentry\SentrySdk; |
| 9 | +use Sentry\State\Scope; |
| 10 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 11 | + |
| 12 | +/** |
| 13 | + * This integration decides whether an event should not be captured according |
| 14 | + * to a series of options that must match with its data. |
| 15 | + * |
| 16 | + * @author Stefano Arlandini <[email protected]> |
| 17 | + */ |
| 18 | +final class IgnoreErrorsIntegration implements IntegrationInterface |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var array<string, mixed> The options |
| 22 | + */ |
| 23 | + private $options; |
| 24 | + |
| 25 | + /** |
| 26 | + * Creates a new instance of this integration and configures it with the |
| 27 | + * given options. |
| 28 | + * |
| 29 | + * @param array<string, mixed> $options The options |
| 30 | + * |
| 31 | + * @psalm-param array{ |
| 32 | + * ignore_exceptions?: bool |
| 33 | + * } $options |
| 34 | + */ |
| 35 | + public function __construct(array $options = []) |
| 36 | + { |
| 37 | + $resolver = new OptionsResolver(); |
| 38 | + $resolver->setDefaults([ |
| 39 | + 'ignore_exceptions' => [], |
| 40 | + ]); |
| 41 | + |
| 42 | + $resolver->setAllowedTypes('ignore_exceptions', ['array']); |
| 43 | + |
| 44 | + $this->options = $resolver->resolve($options); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * {@inheritdoc} |
| 49 | + */ |
| 50 | + public function setupOnce(): void |
| 51 | + { |
| 52 | + Scope::addGlobalEventProcessor(function (Event $event): ?Event { |
| 53 | + $currentHub = SentrySdk::getCurrentHub(); |
| 54 | + $integration = $currentHub->getIntegration(self::class); |
| 55 | + |
| 56 | + if (null !== $integration && $integration->shouldDropEvent($event, $integration->options)) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + return $event; |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Checks whether the given event should be dropped according to the options |
| 66 | + * that configures the current instance of this integration. |
| 67 | + * |
| 68 | + * @param Event $event The event to check |
| 69 | + * @param array<string, mixed> $options The options of the integration |
| 70 | + */ |
| 71 | + private function shouldDropEvent(Event $event, array $options): bool |
| 72 | + { |
| 73 | + if ($this->isIgnoredException($event, $options)) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Checks whether the given event should be dropped or not according to the |
| 82 | + * criteria defined in the integration's options. |
| 83 | + * |
| 84 | + * @param Event $event The event instance |
| 85 | + * @param array<string, mixed> $options The options of the integration |
| 86 | + */ |
| 87 | + private function isIgnoredException(Event $event, array $options): bool |
| 88 | + { |
| 89 | + $exceptions = $event->getExceptions(); |
| 90 | + |
| 91 | + if (empty($exceptions) || !isset($exceptions[0]['type'])) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + foreach ($options['ignore_exceptions'] as $ignoredException) { |
| 96 | + if (is_a($exceptions[0]['type'], $ignoredException, true)) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return false; |
| 102 | + } |
| 103 | +} |
0 commit comments