diff --git a/DependencyInjection/ClientFactory.php b/DependencyInjection/ClientFactory.php index 6bf8be7..0b7d086 100644 --- a/DependencyInjection/ClientFactory.php +++ b/DependencyInjection/ClientFactory.php @@ -139,27 +139,35 @@ class ClientFactory */ protected $filters; + /** + * The exception excepts to not report. + * + * @var string[]|null + */ + protected $excepts; + /** * Create a new client factory instance. * - * @param \Bugsnag\BugsnagBundle\Request\SymfonyResolver $resolver + * @param \Bugsnag\BugsnagBundle\Request\SymfonyResolver $resolver * @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface|null $tokens - * @param \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface|null $checker - * @param string|null $key - * @param string|null $endpoint - * @param bool $callbacks - * @param bool $user - * @param string|null $type - * @param string|null $version - * @param bool $batch - * @param string|null $hostname - * @param bool $code - * @param string|null $strip - * @param string|null $project - * @param string|null $root - * @param string|null $stage - * @param string[]|null $stages - * @param string[]|null $filters + * @param \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface|null $checker + * @param string|null $key + * @param string|null $endpoint + * @param bool $callbacks + * @param bool $user + * @param string|null $type + * @param string|null $version + * @param bool $batch + * @param string|null $hostname + * @param bool $code + * @param string|null $strip + * @param string|null $project + * @param string|null $root + * @param string|null $stage + * @param string[]|null $stages + * @param string[]|null $filters + * @param string[]|null $excepts * * @return void */ @@ -181,8 +189,10 @@ public function __construct( $root = null, $stage = null, array $stages = null, - array $filters = null - ) { + array $filters = null, + array $excepts = null + ) + { $this->resolver = $resolver; $this->tokens = $tokens; $this->checker = $checker; @@ -201,6 +211,7 @@ public function __construct( $this->stage = $stage; $this->stages = $stages; $this->filters = $filters; + $this->excepts = $excepts; } /** @@ -254,9 +265,9 @@ public function make() /** * Setup user detection. * - * @param \Bugsnag\Client $client + * @param \Bugsnag\Client $client * @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface $tokens - * @param \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface $checker + * @param \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface $checker * * @return void */ @@ -275,7 +286,7 @@ protected function setupUserDetection(Client $client, TokenStorageInterface $tok return ['id' => $user->getUsername()]; } - return ['id' => (string) $user]; + return ['id' => (string)$user]; })); } @@ -283,9 +294,9 @@ protected function setupUserDetection(Client $client, TokenStorageInterface $tok * Setup the client paths. * * @param \Bugsnag\Client $client - * @param string|null $strip - * @param string|null $project - * @param string|null $root + * @param string|null $strip + * @param string|null $project + * @param string|null $root * * @return void */ @@ -321,4 +332,15 @@ protected function setupPaths(Client $client, $strip, $project, $root) } } } + + + /** + * @return array|\string[] + */ + public function getExcepts() + { + if (is_array($this->excepts)) + return $this->excepts; + return []; + } } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ee287e1..0f07c9e 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -75,6 +75,11 @@ public function getConfigTreeBuilder() ->treatNullLike([]) ->defaultValue([]) ->end() + ->arrayNode('notify_excepts') + ->prototype('scalar')->end() + ->treatNullLike([]) + ->defaultValue([]) + ->end() ->arrayNode('filters') ->prototype('scalar')->end() ->treatNullLike([]) diff --git a/EventListener/BugsnagListener.php b/EventListener/BugsnagListener.php index 9d235f7..c538a77 100644 --- a/EventListener/BugsnagListener.php +++ b/EventListener/BugsnagListener.php @@ -2,6 +2,7 @@ namespace Bugsnag\BugsnagBundle\EventListener; +use Bugsnag\BugsnagBundle\DependencyInjection\ClientFactory; use Bugsnag\BugsnagBundle\Request\SymfonyResolver; use Bugsnag\Client; use Bugsnag\Report; @@ -33,20 +34,22 @@ class BugsnagListener */ protected $auto; + protected $clientFactory; + /** - * Create a new bugsnag listener instance. - * - * @param \Bugsnag\Client $client - * @param \Bugsnag\BugsnagBundle\Request\SymfonyResolver $resolver - * @param bool $auto - * - * @return void + * BugsnagListener constructor. + * @param Client $client + * @param SymfonyResolver $resolver + * @param $auto + * @param ClientFactory $clientFactory */ - public function __construct(Client $client, SymfonyResolver $resolver, $auto) + public function __construct(Client $client, SymfonyResolver $resolver, $auto, ClientFactory $clientFactory) { + $this->client = $client; $this->resolver = $resolver; $this->auto = $auto; + $this->clientFactory = $clientFactory; } /** @@ -76,13 +79,14 @@ public function onKernelRequest(GetResponseEvent $event) */ public function onKernelException(GetResponseForExceptionEvent $event) { + if (!$this->auto) { return; } $exception = $event->getException(); - $this->client->notifyException($exception); + $this->notifyException($exception); } /** @@ -107,8 +111,24 @@ public function onConsoleException(ConsoleExceptionEvent $event) ], ]; - $this->client->notifyException($exception, function (Report $report) use ($meta) { - $report->setMetaData($meta); - }); + $this->notifyException($exception, $meta); + } + + + private function notifyException($exception, $meta = null) + { + $excepts = $this->clientFactory->getExcepts(); + + if (in_array(get_class($exception), $excepts)) { + return; + } + + if (empty($meta)) { + $this->client->notifyException($exception); + } else { + $this->client->notifyException($exception, function (Report $report) use ($meta) { + $report->setMetaData($meta); + }); + } } } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 4668f8b..ebd5bce 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -23,6 +23,7 @@ services: - '%kernel.environment%' - '%bugsnag.notify_release_stages%' - '%bugsnag.filters%' + - '%bugsnag.notify_excepts%' bugsnag: class: '%bugsnag.client%' @@ -34,6 +35,7 @@ services: - '@bugsnag' - '@bugsnag.resolver' - '%bugsnag.auto_notify%' + - '@bugsnag.factory' tags: - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 256 } - { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: 128 } diff --git a/composer.json b/composer.json index 3d9b92f..4bbda6c 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,15 @@ { - "name": "bugsnag/bugsnag-symfony", - "description": "Official Bugsnag notifier for Symfony applications.", + "name": "roboticsexpert/bugsnag-symfony", + "description": "roboticsexpert Bugsnag notifier for Symfony applications.", "keywords": ["bugsnag", "exceptions", "errors", "logging", "tracking", "symfony"], - "homepage": "https://github.com/bugsnag/bugsnag-symfony", + "homepage": "https://github.com/roboticsexpert/bugsnag-symfony", "type": "symfony-bundle", "license": "MIT", "authors": [ + { + "name": "Mahdi Youseftabar", + "email": "roboticsexpert@gmail.com" + }, { "name": "James Smith", "email": "notifiers@bugsnag.com"