Skip to content

Commit 5f67fd3

Browse files
authored
Fix capture of console errors when the register_error_listener option is disabled (#428)
1 parent ecc5899 commit 5f67fd3

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Add missing `capture-soft-fails` option to the XSD schema for the XML config (#417)
66
- Fix regression that send PII even when the `send_default_pii` option is off (#425)
7+
- Fix capture of console errors when the `register_error_listener` option is disabled (#427)
78

89
## 4.0.0 (2021-01-19)
910

src/DependencyInjection/SentryExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Sentry\Integration\RequestFetcherInterface;
1313
use Sentry\Integration\RequestIntegration;
1414
use Sentry\Options;
15+
use Sentry\SentryBundle\EventListener\ConsoleCommandListener;
1516
use Sentry\SentryBundle\EventListener\ErrorListener;
1617
use Sentry\SentryBundle\EventListener\MessengerListener;
1718
use Sentry\SentryBundle\SentryBundle;
@@ -133,6 +134,8 @@ private function registerErrorListenerConfiguration(ContainerBuilder $container,
133134
if (!$config['register_error_listener']) {
134135
$container->removeDefinition(ErrorListener::class);
135136
}
137+
138+
$container->getDefinition(ConsoleCommandListener::class)->setArgument(1, $config['register_error_listener']);
136139
}
137140

138141
/**

src/EventListener/ConsoleCommandListener.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@ final class ConsoleCommandListener
2121
*/
2222
private $hub;
2323

24+
/**
25+
* @var bool Whether to capture console errors
26+
*/
27+
private $captureErrors;
28+
2429
/**
2530
* Constructor.
2631
*
27-
* @param HubInterface $hub The current hub
32+
* @param HubInterface $hub The current hub
33+
* @param bool $captureErrors Whether to capture console errors
2834
*/
29-
public function __construct(HubInterface $hub)
35+
public function __construct(HubInterface $hub, bool $captureErrors = true)
3036
{
3137
$this->hub = $hub;
38+
$this->captureErrors = $captureErrors;
3239
}
3340

3441
/**
@@ -66,7 +73,9 @@ public function handleConsoleErrorEvent(ConsoleErrorEvent $event): void
6673
$this->hub->configureScope(function (Scope $scope) use ($event): void {
6774
$scope->setTag('console.command.exit_code', (string) $event->getExitCode());
6875

69-
$this->hub->captureException($event->getError());
76+
if ($this->captureErrors) {
77+
$this->hub->captureException($event->getError());
78+
}
7079
});
7180
}
7281
}

tests/DependencyInjection/SentryExtensionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ public function testConsoleCommandListener(): void
7979
],
8080
],
8181
], $definition->getTags());
82+
83+
$this->assertTrue($definition->getArgument(1));
84+
}
85+
86+
public function testConsoleCommandListenerDoesNotCaptureErrorsWhenErrorListenerIsDisabled(): void
87+
{
88+
$container = $this->createContainerFromFixture('error_listener_disabled');
89+
$definition = $container->getDefinition(ConsoleCommandListener::class);
90+
91+
$this->assertFalse($definition->getArgument(1));
8292
}
8393

8494
public function testMessengerListener(): void

tests/EventListener/ConsoleCommandListenerTest.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,9 @@ final class ConsoleCommandListenerTest extends TestCase
2424
*/
2525
private $hub;
2626

27-
/**
28-
* @var ConsoleCommandListener
29-
*/
30-
private $listener;
31-
3227
protected function setUp(): void
3328
{
3429
$this->hub = $this->createMock(HubInterface::class);
35-
$this->listener = new ConsoleCommandListener($this->hub);
3630
}
3731

3832
/**
@@ -43,12 +37,13 @@ protected function setUp(): void
4337
public function testHandleConsoleCommandEvent(ConsoleCommandEvent $consoleEvent, array $expectedTags): void
4438
{
4539
$scope = new Scope();
40+
$listener = new ConsoleCommandListener($this->hub);
4641

4742
$this->hub->expects($this->once())
4843
->method('pushScope')
4944
->willReturn($scope);
5045

51-
$this->listener->handleConsoleCommandEvent($consoleEvent);
46+
$listener->handleConsoleCommandEvent($consoleEvent);
5247

5348
$event = $scope->applyToEvent(Event::createEvent());
5449

@@ -78,31 +73,46 @@ public function handleConsoleCommmandEventDataProvider(): \Generator
7873

7974
public function testHandleConsoleTerminateEvent(): void
8075
{
76+
$listener = new ConsoleCommandListener($this->hub);
77+
8178
$this->hub->expects($this->once())
8279
->method('popScope');
8380

84-
$this->listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(new Command(), $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class), 0));
81+
$listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(new Command(), $this->createMock(InputInterface::class), $this->createMock(OutputInterface::class), 0));
8582
}
8683

87-
public function testHandleConsoleErrorEvent(): void
84+
/**
85+
* @dataProvider handleConsoleErrorEventDataProvider
86+
*/
87+
public function testHandleConsoleErrorEvent(bool $captureErrors): void
8888
{
8989
$scope = new Scope();
9090
$consoleEvent = new ConsoleErrorEvent($this->createMock(InputInterface::class), $this->createMock(OutputInterface::class), new \Exception());
91+
$listener = new ConsoleCommandListener($this->hub, $captureErrors);
9192

9293
$this->hub->expects($this->once())
9394
->method('configureScope')
9495
->willReturnCallback(static function (callable $callback) use ($scope): void {
9596
$callback($scope);
9697
});
9798

98-
$this->hub->expects($this->once())
99+
$this->hub->expects($captureErrors ? $this->once() : $this->never())
99100
->method('captureException')
100101
->with($consoleEvent->getError());
101102

102-
$this->listener->handleConsoleErrorEvent($consoleEvent);
103+
$listener->handleConsoleErrorEvent($consoleEvent);
103104

104105
$event = $scope->applyToEvent(Event::createEvent());
105106

106107
$this->assertSame(['console.command.exit_code' => '1'], $event->getTags());
107108
}
109+
110+
/**
111+
* @return \Generator<mixed>
112+
*/
113+
public function handleConsoleErrorEventDataProvider(): \Generator
114+
{
115+
yield [true];
116+
yield [false];
117+
}
108118
}

0 commit comments

Comments
 (0)