Skip to content

Commit 9681cb6

Browse files
committed
Add BC trick to avoid issues with Symfony 3.x
1 parent 4999bcd commit 9681cb6

File tree

6 files changed

+103
-31
lines changed

6 files changed

+103
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111
- Add support for Symfony 4.x
1212
### Changed
1313
- The `SentryBundle::VERSION` constant has been replaced with the `SentryBundle::getVersion(): string` method, to get a more accurate result
14-
- Due to a deprecation in `symfony/console`, we require it at at least version 3.3, and we made this change to the `SentryExceptionListenerInterface`:
14+
- Due to a deprecation in `symfony/console`, we require it at at least version 3.3, and we added a method to `SentryExceptionListenerInterface`:
1515
```php
16-
// before
17-
public function onConsoleException(ConsoleExceptionEvent $event);
18-
// after
1916
public function onConsoleException(ConsoleErrorEvent $event);
2017
```
2118
### Removed

src/EventListener/ExceptionListener.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Sentry\SentryBundle\SentrySymfonyEvents;
77
use Symfony\Component\Console\Event\ConsoleCommandEvent;
88
use Symfony\Component\Console\Event\ConsoleErrorEvent;
9+
use Symfony\Component\Console\Event\ConsoleEvent;
10+
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
911
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1012
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1113
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
@@ -119,14 +121,29 @@ public function onConsoleCommand(ConsoleCommandEvent $event): void
119121
// only triggers loading of client, does not need to do anything.
120122
}
121123

122-
/**
123-
* @param ConsoleErrorEvent $event
124-
*/
125-
public function onConsoleException(ConsoleErrorEvent $event): void
124+
public function onConsoleError(ConsoleErrorEvent $event): void
125+
{
126+
$this->handleConsoleError($event);
127+
}
128+
129+
public function onConsoleException(ConsoleExceptionEvent $event): void
130+
{
131+
$this->handleConsoleError($event);
132+
}
133+
134+
private function handleConsoleError(ConsoleEvent $event): void
126135
{
127136
$command = $event->getCommand();
128-
/** @var \Exception $exception to avoid issues with PHPStan */
129-
$exception = $event->getError();
137+
switch (true) {
138+
case $event instanceof ConsoleErrorEvent:
139+
$exception = $event->getError();
140+
break;
141+
case $event instanceof ConsoleExceptionEvent:
142+
$exception = $event->getException();
143+
break;
144+
default:
145+
throw new \InvalidArgumentException('Event not recognized: ' . \get_class($event));
146+
}
130147

131148
if ($this->shouldExceptionCaptureBeSkipped($exception)) {
132149
return;

src/EventListener/SentryExceptionListenerInterface.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Sentry\SentryBundle\EventListener;
44

55
use Symfony\Component\Console\Event\ConsoleErrorEvent;
6+
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
67
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
78
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
89

@@ -33,5 +34,14 @@ public function onKernelException(GetResponseForExceptionEvent $event): void;
3334
*
3435
* @param ConsoleErrorEvent $event
3536
*/
36-
public function onConsoleException(ConsoleErrorEvent $event): void;
37+
public function onConsoleError(ConsoleErrorEvent $event): void;
38+
39+
/**
40+
* When an exception occurs on the command line, this method will be
41+
* triggered for capturing the error.
42+
*
43+
* @param ConsoleErrorEvent $event
44+
* @deprecated This method exists for BC with Symfony 3.x
45+
*/
46+
public function onConsoleException(ConsoleExceptionEvent $event): void;
3747
}

src/Resources/config/services.yml

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
services:
2-
sentry.client:
3-
class: '%sentry.client%'
4-
arguments: ['%sentry.dsn%', '%sentry.options%']
5-
public: true
6-
calls:
7-
- [install, []]
2+
sentry.client:
3+
class: '%sentry.client%'
4+
arguments: ['%sentry.dsn%', '%sentry.options%']
5+
public: true
6+
calls:
7+
- [install, []]
88

9-
sentry.exception_listener:
10-
class: '%sentry.exception_listener%'
11-
arguments:
12-
- '@sentry.client'
13-
- '@event_dispatcher'
14-
- '%sentry.skip_capture%'
15-
- '@?security.token_storage'
16-
- '@?security.authorization_checker'
17-
tags:
18-
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: '%sentry.listener_priorities.request%'}
19-
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: '%sentry.listener_priorities.kernel_exception%' }
20-
- { name: kernel.event_listener, event: console.command, method: onConsoleCommand }
21-
- { name: kernel.event_listener, event: console.exception, method: onConsoleException, priority: '%sentry.listener_priorities.console_exception%' }
9+
sentry.exception_listener:
10+
class: '%sentry.exception_listener%'
11+
arguments:
12+
- '@sentry.client'
13+
- '@event_dispatcher'
14+
- '%sentry.skip_capture%'
15+
- '@?security.token_storage'
16+
- '@?security.authorization_checker'
17+
tags:
18+
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: '%sentry.listener_priorities.request%'}
19+
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: '%sentry.listener_priorities.kernel_exception%' }
20+
- { name: kernel.event_listener, event: console.command, method: onConsoleCommand }
21+
- { name: kernel.event_listener, event: console.exception, method: onConsoleException, priority: '%sentry.listener_priorities.console_exception%' }
22+
- { name: kernel.event_listener, event: console.error, method: onConsoleError, priority: '%sentry.listener_priorities.console_exception%' }

test/DependencyInjection/SentryExtensionTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ public function test_that_it_has_proper_event_listener_tags_for_exception_listen
292292
'method' => 'onConsoleException',
293293
'priority' => '%sentry.listener_priorities.console_exception%',
294294
],
295+
[
296+
'event' => 'console.error',
297+
'method' => 'onConsoleError',
298+
'priority' => '%sentry.listener_priorities.console_exception%',
299+
],
295300
],
296301
$tags
297302
);

test/EventListener/ExceptionListenerTest.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Sentry\SentryBundle\SentrySymfonyEvents;
1111
use Symfony\Component\Console\Command\Command;
1212
use Symfony\Component\Console\Event\ConsoleErrorEvent;
13+
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
1314
use Symfony\Component\Console\Input\InputInterface;
1415
use Symfony\Component\Console\Output\OutputInterface;
1516
use Symfony\Component\DependencyInjection\Alias;
@@ -459,6 +460,47 @@ public function test_that_it_captures_exception()
459460
* @dataProvider mockCommandProvider
460461
*/
461462
public function test_that_it_captures_console_exception(?Command $mockCommand, string $expectedCommandName)
463+
{
464+
if (null === $mockCommand) {
465+
$this->markTestSkipped('Command missing is not possibile with ConsoleExceptionEvent');
466+
}
467+
468+
$exception = $this->createMock(\Exception::class);
469+
/** @var InputInterface $input */
470+
$input = $this->createMock(InputInterface::class);
471+
/** @var OutputInterface $output */
472+
$output = $this->createMock(OutputInterface::class);
473+
474+
$event = new ConsoleExceptionEvent($mockCommand, $input, $output, $exception, 10);
475+
476+
$this->mockEventDispatcher
477+
->expects($this->once())
478+
->method('dispatch')
479+
->with($this->identicalTo(SentrySymfonyEvents::PRE_CAPTURE), $this->identicalTo($event));
480+
481+
$this->mockSentryClient
482+
->expects($this->once())
483+
->method('captureException')
484+
->with(
485+
$this->identicalTo($exception),
486+
$this->identicalTo([
487+
'tags' => [
488+
'command' => $expectedCommandName,
489+
'status_code' => 10,
490+
],
491+
])
492+
);
493+
494+
$this->containerBuilder->compile();
495+
/** @var SentryExceptionListenerInterface $listener */
496+
$listener = $this->getListener();
497+
$listener->onConsoleException($event);
498+
}
499+
500+
/**
501+
* @dataProvider mockCommandProvider
502+
*/
503+
public function test_that_it_captures_console_error(?Command $mockCommand, string $expectedCommandName)
462504
{
463505
$error = $this->createMock(\Error::class);
464506
/** @var InputInterface $input */
@@ -490,7 +532,7 @@ public function test_that_it_captures_console_exception(?Command $mockCommand, s
490532
$this->containerBuilder->compile();
491533
/** @var SentryExceptionListenerInterface $listener */
492534
$listener = $this->getListener();
493-
$listener->onConsoleException($event);
535+
$listener->onConsoleError($event);
494536
}
495537

496538
public function mockCommandProvider()

0 commit comments

Comments
 (0)