Skip to content

Commit f21d3f8

Browse files
committed
Add ability to disable ErrorListener
1 parent aa7d969 commit f21d3f8

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

src/DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function getConfigTreeBuilder(): TreeBuilder
3737
->ifString()
3838
->then($this->getTrimClosure());
3939

40+
$rootNode->children()
41+
->booleanNode('register_error_listener')
42+
->defaultTrue();
43+
4044
// Options array (to be passed to Sentry\Options constructor) -- please keep alphabetical order!
4145
$optionsNode = $rootNode->children()
4246
->arrayNode('options')

src/DependencyInjection/SentryExtension.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function load(array $configs, ContainerBuilder $container): void
4747
$container->setParameter('sentry.listener_priorities.' . $key, $priority);
4848
}
4949

50-
$this->tagConsoleErrorListener($container);
50+
$this->configureErrorListener($container, $processedConfiguration);
5151
$this->setLegacyVisibilities($container);
5252
}
5353

@@ -134,6 +134,17 @@ private function valueToCallable($value)
134134
return $value;
135135
}
136136

137+
private function configureErrorListener(ContainerBuilder $container, array $processedConfiguration): void
138+
{
139+
if (! $processedConfiguration['register_error_listener']) {
140+
$container->removeDefinition(ErrorListener::class);
141+
142+
return;
143+
}
144+
145+
$this->tagConsoleErrorListener($container);
146+
}
147+
137148
/**
138149
* BC layer for Symfony < 3.3; see https://symfony.com/blog/new-in-symfony-3-3-better-handling-of-command-exceptions
139150
*/
@@ -166,9 +177,12 @@ private function setLegacyVisibilities(ContainerBuilder $container): void
166177
if (Kernel::VERSION_ID < 30300) {
167178
$container->getDefinition(SentryTestCommand::class)->setPublic(true);
168179
$container->getDefinition(ConsoleListener::class)->setPublic(true);
169-
$container->getDefinition(ErrorListener::class)->setPublic(true);
170180
$container->getDefinition(RequestListener::class)->setPublic(true);
171181
$container->getDefinition(SubRequestListener::class)->setPublic(true);
182+
183+
if ($container->hasDefinition(ErrorListener::class)) {
184+
$container->getDefinition(ErrorListener::class)->setPublic(true);
185+
}
172186
}
173187
}
174188
}

test/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function testConfigurationDefaults(): void
4848
$processed = $this->processConfiguration([]);
4949
$expectedDefaults = [
5050
'dsn' => null,
51+
'register_error_listener' => true,
5152
'listener_priorities' => [
5253
'request' => 1,
5354
'sub_request' => 1,

test/DependencyInjection/SentryExtensionTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Sentry\Integration\IntegrationInterface;
99
use Sentry\Options;
1010
use Sentry\SentryBundle\DependencyInjection\SentryExtension;
11+
use Sentry\SentryBundle\EventListener\ErrorListener;
1112
use Sentry\SentryBundle\Test\BaseTestCase;
1213
use Symfony\Component\DependencyInjection\Alias;
1314
use Symfony\Component\DependencyInjection\Container;
@@ -20,6 +21,7 @@
2021
class SentryExtensionTest extends BaseTestCase
2122
{
2223
private const OPTIONS_TEST_PUBLIC_ALIAS = 'sentry.options.public_alias';
24+
private const ERROR_LISTENER_TEST_PUBLIC_ALIAS = 'sentry.error_listener.public_alias';
2325

2426
public function testDataProviderIsMappingTheRightNumberOfOptions(): void
2527
{
@@ -335,6 +337,26 @@ public function testIntegrations(): void
335337
$this->assertCount(1, $integrations);
336338
}
337339

340+
/**
341+
* @dataProvider errorListenerConfigurationProvider
342+
*/
343+
public function testErrorListenerIsRegistered(bool $registerErrorListener): void
344+
{
345+
$container = $this->getContainer([
346+
'register_error_listener' => $registerErrorListener,
347+
]);
348+
349+
$this->assertEquals($registerErrorListener, $container->has(self::ERROR_LISTENER_TEST_PUBLIC_ALIAS));
350+
}
351+
352+
public function errorListenerConfigurationProvider(): array
353+
{
354+
return [
355+
[true],
356+
[false],
357+
];
358+
}
359+
338360
private function getContainer(array $configuration = []): Container
339361
{
340362
$containerBuilder = new ContainerBuilder();
@@ -363,6 +385,10 @@ private function getContainer(array $configuration = []): Container
363385
$extension = new SentryExtension();
364386
$extension->load(['sentry' => $configuration], $containerBuilder);
365387

388+
if ($containerBuilder->hasDefinition(ErrorListener::class)) {
389+
$containerBuilder->setAlias(self::ERROR_LISTENER_TEST_PUBLIC_ALIAS, new Alias(ErrorListener::class, true));
390+
}
391+
366392
$containerBuilder->compile();
367393

368394
return $containerBuilder;

0 commit comments

Comments
 (0)