diff --git a/CHANGELOG.md b/CHANGELOG.md index b2dff994..f51a457c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Add missing `capture-soft-fails` option to the XSD schema for the XML config (#417) - Fix regression that send PII even when the `send_default_pii` option is off (#425) +- Fix capture of console errors when the `register_error_listener` option is disabled (#427) ## 4.0.0 (2021-01-19) diff --git a/src/DependencyInjection/SentryExtension.php b/src/DependencyInjection/SentryExtension.php index 302e432a..89f8a928 100644 --- a/src/DependencyInjection/SentryExtension.php +++ b/src/DependencyInjection/SentryExtension.php @@ -12,6 +12,7 @@ use Sentry\Integration\RequestFetcherInterface; use Sentry\Integration\RequestIntegration; use Sentry\Options; +use Sentry\SentryBundle\EventListener\ConsoleCommandListener; use Sentry\SentryBundle\EventListener\ErrorListener; use Sentry\SentryBundle\EventListener\MessengerListener; use Sentry\SentryBundle\SentryBundle; @@ -133,6 +134,8 @@ private function registerErrorListenerConfiguration(ContainerBuilder $container, if (!$config['register_error_listener']) { $container->removeDefinition(ErrorListener::class); } + + $container->getDefinition(ConsoleCommandListener::class)->setArgument(1, $config['register_error_listener']); } /** diff --git a/src/EventListener/ConsoleCommandListener.php b/src/EventListener/ConsoleCommandListener.php index 4fecba07..a32137cd 100644 --- a/src/EventListener/ConsoleCommandListener.php +++ b/src/EventListener/ConsoleCommandListener.php @@ -21,14 +21,21 @@ final class ConsoleCommandListener */ private $hub; + /** + * @var bool Whether to capture console errors + */ + private $captureErrors; + /** * Constructor. * - * @param HubInterface $hub The current hub + * @param HubInterface $hub The current hub + * @param bool $captureErrors Whether to capture console errors */ - public function __construct(HubInterface $hub) + public function __construct(HubInterface $hub, bool $captureErrors = true) { $this->hub = $hub; + $this->captureErrors = $captureErrors; } /** @@ -66,7 +73,9 @@ public function handleConsoleErrorEvent(ConsoleErrorEvent $event): void $this->hub->configureScope(function (Scope $scope) use ($event): void { $scope->setTag('console.command.exit_code', (string) $event->getExitCode()); - $this->hub->captureException($event->getError()); + if ($this->captureErrors) { + $this->hub->captureException($event->getError()); + } }); } } diff --git a/tests/DependencyInjection/SentryExtensionTest.php b/tests/DependencyInjection/SentryExtensionTest.php index e59a575e..6c848869 100644 --- a/tests/DependencyInjection/SentryExtensionTest.php +++ b/tests/DependencyInjection/SentryExtensionTest.php @@ -79,6 +79,16 @@ public function testConsoleCommandListener(): void ], ], ], $definition->getTags()); + + $this->assertTrue($definition->getArgument(1)); + } + + public function testConsoleCommandListenerDoesNotCaptureErrorsWhenErrorListenerIsDisabled(): void + { + $container = $this->createContainerFromFixture('error_listener_disabled'); + $definition = $container->getDefinition(ConsoleCommandListener::class); + + $this->assertFalse($definition->getArgument(1)); } public function testMessengerListener(): void diff --git a/tests/EventListener/ConsoleCommandListenerTest.php b/tests/EventListener/ConsoleCommandListenerTest.php index b7dc49ee..c4d2aee8 100644 --- a/tests/EventListener/ConsoleCommandListenerTest.php +++ b/tests/EventListener/ConsoleCommandListenerTest.php @@ -24,15 +24,9 @@ final class ConsoleCommandListenerTest extends TestCase */ private $hub; - /** - * @var ConsoleCommandListener - */ - private $listener; - protected function setUp(): void { $this->hub = $this->createMock(HubInterface::class); - $this->listener = new ConsoleCommandListener($this->hub); } /** @@ -43,12 +37,13 @@ protected function setUp(): void public function testHandleConsoleCommandEvent(ConsoleCommandEvent $consoleEvent, array $expectedTags): void { $scope = new Scope(); + $listener = new ConsoleCommandListener($this->hub); $this->hub->expects($this->once()) ->method('pushScope') ->willReturn($scope); - $this->listener->handleConsoleCommandEvent($consoleEvent); + $listener->handleConsoleCommandEvent($consoleEvent); $event = $scope->applyToEvent(Event::createEvent()); @@ -78,16 +73,22 @@ public function handleConsoleCommmandEventDataProvider(): \Generator public function testHandleConsoleTerminateEvent(): void { + $listener = new ConsoleCommandListener($this->hub); + $this->hub->expects($this->once()) ->method('popScope'); - $this->listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(new Command(), $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class), 0)); + $listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(new Command(), $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class), 0)); } - public function testHandleConsoleErrorEvent(): void + /** + * @dataProvider handleConsoleErrorEventDataProvider + */ + public function testHandleConsoleErrorEvent(bool $captureErrors): void { $scope = new Scope(); $consoleEvent = new ConsoleErrorEvent($this->createMock(InputInterface::class), $this->createMock(OutputInterface::class), new \Exception()); + $listener = new ConsoleCommandListener($this->hub, $captureErrors); $this->hub->expects($this->once()) ->method('configureScope') @@ -95,14 +96,23 @@ public function testHandleConsoleErrorEvent(): void $callback($scope); }); - $this->hub->expects($this->once()) + $this->hub->expects($captureErrors ? $this->once() : $this->never()) ->method('captureException') ->with($consoleEvent->getError()); - $this->listener->handleConsoleErrorEvent($consoleEvent); + $listener->handleConsoleErrorEvent($consoleEvent); $event = $scope->applyToEvent(Event::createEvent()); $this->assertSame(['console.command.exit_code' => '1'], $event->getTags()); } + + /** + * @return \Generator + */ + public function handleConsoleErrorEventDataProvider(): \Generator + { + yield [true]; + yield [false]; + } }