Skip to content

Commit

Permalink
Fix capture of console errors when the register_error_listener option…
Browse files Browse the repository at this point in the history
… is disabled (#428)
  • Loading branch information
ste93cry authored Jan 26, 2021
1 parent ecc5899 commit 5f67fd3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions src/DependencyInjection/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
}

/**
Expand Down
15 changes: 12 additions & 3 deletions src/EventListener/ConsoleCommandListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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());
}
});
}
}
10 changes: 10 additions & 0 deletions tests/DependencyInjection/SentryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 21 additions & 11 deletions tests/EventListener/ConsoleCommandListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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());

Expand Down Expand Up @@ -78,31 +73,46 @@ 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')
->willReturnCallback(static function (callable $callback) use ($scope): 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<mixed>
*/
public function handleConsoleErrorEventDataProvider(): \Generator
{
yield [true];
yield [false];
}
}

0 comments on commit 5f67fd3

Please sign in to comment.