Skip to content

Commit cd15792

Browse files
committed
Remove deprecation from Symfony exception event listener
1 parent de483aa commit cd15792

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/DependencyInjection/SentryExtension.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1616
use Symfony\Component\Config\FileLocator;
1717
use Symfony\Component\Console\ConsoleEvents;
18+
use Symfony\Component\Console\Event\ConsoleErrorEvent;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
1920
use Symfony\Component\DependencyInjection\Exception\LogicException;
2021
use Symfony\Component\DependencyInjection\Loader;
2122
use Symfony\Component\DependencyInjection\Reference;
2223
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
24+
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
2325
use Symfony\Component\HttpKernel\Kernel;
26+
use Symfony\Component\HttpKernel\KernelEvents;
2427

2528
/**
2629
* This is the class that loads and manages your bundle configuration
@@ -146,17 +149,37 @@ private function configureErrorListener(ContainerBuilder $container, array $proc
146149
return;
147150
}
148151

152+
$this->tagExceptionListener($container);
149153
$this->tagConsoleErrorListener($container);
150154
}
151155

156+
/**
157+
* BC layer for Symfony < 4.3
158+
*/
159+
private function tagExceptionListener(ContainerBuilder $container): void
160+
{
161+
$listener = $container->getDefinition(ErrorListener::class);
162+
$method = class_exists(ExceptionEvent::class)
163+
? 'onException'
164+
: 'onKernelException';
165+
166+
$tagAttributes = [
167+
'event' => KernelEvents::EXCEPTION,
168+
'method' => $method,
169+
'priority' => '%sentry.listener_priorities.console_error%',
170+
];
171+
172+
$listener->addTag('kernel.event_listener', $tagAttributes);
173+
}
174+
152175
/**
153176
* BC layer for Symfony < 3.3; see https://symfony.com/blog/new-in-symfony-3-3-better-handling-of-command-exceptions
154177
*/
155178
private function tagConsoleErrorListener(ContainerBuilder $container): void
156179
{
157180
$listener = $container->getDefinition(ErrorListener::class);
158181

159-
if (class_exists('Symfony\Component\Console\Event\ConsoleErrorEvent')) {
182+
if (class_exists(ConsoleErrorEvent::class)) {
160183
$tagAttributes = [
161184
'event' => ConsoleEvents::ERROR,
162185
'method' => 'onConsoleError',

src/EventListener/ErrorListener.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Sentry\State\HubInterface;
66
use Symfony\Component\Console\Event\ConsoleErrorEvent;
77
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
8+
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
89
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
910

1011
final class ErrorListener
@@ -21,6 +22,14 @@ public function __construct(HubInterface $hub)
2122
$this->hub = $hub; // not used, needed to trigger instantiation
2223
}
2324

25+
public function onException(ExceptionEvent $event): void
26+
{
27+
\Sentry\captureException($event->getThrowable());
28+
}
29+
30+
/**
31+
* BC layer for Symfony < 4.3
32+
*/
2433
public function onKernelException(GetResponseForExceptionEvent $event): void
2534
{
2635
\Sentry\captureException($event->getException());

src/Resources/config/services.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232

3333
<service id="Sentry\SentryBundle\EventListener\ErrorListener" class="Sentry\SentryBundle\EventListener\ErrorListener" public="false">
3434
<argument type="service" id="Sentry\State\HubInterface" />
35-
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="%sentry.listener_priorities.request_error%" />
35+
<!-- The following tag is done manually in PHP for BC with Symfony < 4.3, -->
36+
<!-- <tag name="kernel.event_listener" event="kernel.exception" method="onException" priority="%sentry.listener_priorities.request_error%" />-->
3637
<!-- The following tag is done manually in PHP for BC with Symfony < 3.3 -->
3738
<!-- <tag name="kernel.event_listener" event="console.error" method="onConsoleError" priority="%sentry.listener_priorities.console_error%" />-->
3839
</service>

0 commit comments

Comments
 (0)