Skip to content

Commit

Permalink
Use the hub injected in the constructor of the listeners rather than …
Browse files Browse the repository at this point in the history
…retrieving it from the SDK singleton
  • Loading branch information
ste93cry committed Nov 17, 2020
1 parent a28795f commit 26fb461
Show file tree
Hide file tree
Showing 20 changed files with 1,100 additions and 851 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Added `options.traces_sampler` and `options.traces_sample_rate` configuration options (#385)
- [BC BREAK] Removed the `options.project_root` configuration option. Instead of setting it, use a combination of `options.in_app_include` and `options.in_app_exclude` (#385)
- [BC BREAK] Removed the `options.excluded_exceptions` configuration option. Instead of setting it, configure the `IgnoreErrorsIntegration` integration (#385)
- [BC BREAK] Refactorized the `ConsoleCommandListener`, `ErrorListener`, `RequestListener` and `SubRequestListener` event listeners (#387)
- CLI commands registration policy changed to lazy load

## 3.5.3 (2020-10-13)
Expand Down
10 changes: 10 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Upgrade 3.x to 4.0

- Added the `$hub` argument to the constructor of the `SubRequestListener` class.
- Renamed the `ConsoleListener` class to `ConsoleCommandListener`.
- Removed the `ConsoleListener::onConsoleCommand` method.
- Renamed the `ErrorListener::onException` method to `ErrorListener::handleExceptionEvent`.
- Removed the `ErrorListener::onKernelException` method.
- Removed the `ErrorListener::onConsoleError` method.
- Renamed the `RequestListener::onKernelRequest` method to `RequestListener::handleKernelRequestEvent`.
- Renamed the `RequestListener::onKernelController` method to `RequestListener::handleKernelControllerEvent`.
- Renamed the `SubRequestListener::onKernelRequest` method to `SubRequestListener::handleKernelRequestEvent`.
- Renamed the `SubRequestListener::onKernelFinishRequest` method to `SubRequestListener::handleKernelFinishRequestEvent`.
- Removed the `sentry.options.excluded_exceptions` configuration option.

Before:
Expand Down
120 changes: 83 additions & 37 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ parameters:
path: src/DependencyInjection/SentryExtension.php

-
message: "#^Parameter \\$event of method Sentry\\\\SentryBundle\\\\EventListener\\\\ErrorListener\\:\\:onKernelException\\(\\) has invalid typehint type Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseForExceptionEvent\\.$#"
message: "#^If condition is always true\\.$#"
count: 1
path: src/EventListener/ErrorListener.php

-
message: "#^Call to method getException\\(\\) on an unknown class Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseForExceptionEvent\\.$#"
message: "#^Class Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseForExceptionEvent not found\\.$#"
count: 1
path: src/EventListener/ErrorListener.php

-
message: "#^Else branch is unreachable because previous condition is always true\\.$#"
count: 1
path: src/EventListener/ErrorListener.php

Expand All @@ -61,7 +66,17 @@ parameters:
path: src/EventListener/RequestListener.php

-
message: "#^Parameter \\$event of method Sentry\\\\SentryBundle\\\\EventListener\\\\RequestListener\\:\\:onKernelRequest\\(\\) has invalid typehint type Sentry\\\\SentryBundle\\\\EventListener\\\\RequestListenerRequestEvent\\.$#"
message: "#^Class Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseEvent not found\\.$#"
count: 1
path: src/EventListener/RequestListener.php

-
message: "#^Class Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\FilterControllerEvent not found\\.$#"
count: 1
path: src/EventListener/RequestListener.php

-
message: "#^Parameter \\$event of method Sentry\\\\SentryBundle\\\\EventListener\\\\RequestListener\\:\\:handleKernelRequestEvent\\(\\) has invalid typehint type Sentry\\\\SentryBundle\\\\EventListener\\\\RequestListenerRequestEvent\\.$#"
count: 2
path: src/EventListener/RequestListener.php

Expand All @@ -76,8 +91,8 @@ parameters:
path: src/EventListener/RequestListener.php

-
message: "#^Parameter \\$event of method Sentry\\\\SentryBundle\\\\EventListener\\\\RequestListener\\:\\:onKernelController\\(\\) has invalid typehint type Sentry\\\\SentryBundle\\\\EventListener\\\\RequestListenerControllerEvent\\.$#"
count: 1
message: "#^Parameter \\$event of method Sentry\\\\SentryBundle\\\\EventListener\\\\RequestListener\\:\\:handleKernelControllerEvent\\(\\) has invalid typehint type Sentry\\\\SentryBundle\\\\EventListener\\\\RequestListenerControllerEvent\\.$#"
count: 2
path: src/EventListener/RequestListener.php

-
Expand All @@ -87,7 +102,7 @@ parameters:

-
message: "#^Call to method getRequest\\(\\) on an unknown class Sentry\\\\SentryBundle\\\\EventListener\\\\RequestListenerControllerEvent\\.$#"
count: 2
count: 1
path: src/EventListener/RequestListener.php

-
Expand All @@ -106,19 +121,19 @@ parameters:
path: src/EventListener/SubRequestListener.php

-
message: "#^Parameter \\$event of method Sentry\\\\SentryBundle\\\\EventListener\\\\SubRequestListener\\:\\:onKernelRequest\\(\\) has invalid typehint type Sentry\\\\SentryBundle\\\\EventListener\\\\SubRequestListenerRequestEvent\\.$#"
count: 2
message: "#^Class Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseEvent not found\\.$#"
count: 1
path: src/EventListener/SubRequestListener.php

-
message: "#^Call to method isMasterRequest\\(\\) on an unknown class Sentry\\\\SentryBundle\\\\EventListener\\\\SubRequestListenerRequestEvent\\.$#"
count: 1
message: "#^Parameter \\$event of method Sentry\\\\SentryBundle\\\\EventListener\\\\SubRequestListener\\:\\:handleKernelRequestEvent\\(\\) has invalid typehint type Sentry\\\\SentryBundle\\\\EventListener\\\\SubRequestListenerRequestEvent\\.$#"
count: 2
path: src/EventListener/SubRequestListener.php

-
message: "#^Method Sentry\\\\SentryBundle\\\\Test\\\\BaseTestCase\\:\\:createRequestEvent\\(\\) has no return typehint specified\\.$#"
message: "#^Call to method isMasterRequest\\(\\) on an unknown class Sentry\\\\SentryBundle\\\\EventListener\\\\SubRequestListenerRequestEvent\\.$#"
count: 1
path: test/BaseTestCase.php
path: src/EventListener/SubRequestListener.php

-
message: "#^Method Sentry\\\\SentryBundle\\\\Test\\\\DependencyInjection\\\\ConfigurationTest\\:\\:testOptionValuesProcessing\\(\\) has parameter \\$value with no typehint specified\\.$#"
Expand Down Expand Up @@ -226,16 +241,17 @@ parameters:
path: test/DependencyInjection/SentryExtensionTest.php

-
message: "#^Comparison operation \"\\>\\=\" between \\d+ and 40300 is always true\\.$#"
message: "#^Comparison operation \"\\>\\=\" between 50108 and 40300 is always true\\.$#"
count: 1
path: test/End2End/App/Kernel.php

-
message: "#^Class Symfony\\\\Bundle\\\\FrameworkBundle\\\\Client not found\\.$#"
count: 1
path: test/End2End/End2EndTest.php

-
message: "#^Comparison operation \"\\<\" between \\d+ and 40300 is always false\\.$#"
message: "#^Comparison operation \"\\<\" between 50108 and 40300 is always false\\.$#"
count: 1
path: test/End2End/End2EndTest.php

Expand All @@ -250,22 +266,37 @@ parameters:
path: test/ErrorTypesParserTest.php

-
message: "#^Property Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\ConsoleListenerTest\\:\\:\\$currentHub has no typehint specified\\.$#"
message: "#^Parameter \\$event of method Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\ErrorListenerTest\\:\\:testHandleExceptionEvent\\(\\) has invalid typehint type Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseForExceptionEvent\\.$#"
count: 1
path: test/EventListener/ErrorListenerTest.php

-
message: "#^Call to method getException\\(\\) on an unknown class Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseForExceptionEvent\\.$#"
count: 1
path: test/EventListener/ErrorListenerTest.php

-
message: "#^Parameter \\#1 \\$event of method Sentry\\\\SentryBundle\\\\EventListener\\\\ErrorListener\\:\\:handleExceptionEvent\\(\\) expects Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\ExceptionEvent, Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\ExceptionEvent\\|Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseForExceptionEvent given\\.$#"
count: 1
path: test/EventListener/ErrorListenerTest.php

-
message: "#^If condition is always true\\.$#"
count: 1
path: test/EventListener/ConsoleListenerTest.php
path: test/EventListener/ErrorListenerTest.php

-
message: "#^Property Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\ConsoleListenerTest\\:\\:\\$currentScope has no typehint specified\\.$#"
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 1
path: test/EventListener/ConsoleListenerTest.php
path: test/EventListener/ErrorListenerTest.php

-
message: "#^Method Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\ConsoleListenerTest\\:\\:getTagsContext\\(\\) return type has no value type specified in iterable type array\\.$#"
message: "#^If condition is always false\\.$#"
count: 1
path: test/EventListener/ConsoleListenerTest.php
path: test/EventListener/ErrorListenerTest.php

-
message: "#^Class Symfony\\\\Component\\\\Messenger\\\\Event\\\\WorkerMessageHandledEvent constructor invoked with 3 parameters, 2 required\\.$#"
message: "#^If condition is always false\\.$#"
count: 1
path: test/EventListener/MessengerListenerTest.php

Expand All @@ -275,52 +306,67 @@ parameters:
path: test/EventListener/MessengerListenerTest.php

-
message: "#^Property Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\RequestListenerTest\\:\\:\\$currentScope has no typehint specified\\.$#"
message: "#^Parameter \\$requestEvent of method Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\RequestListenerTest\\:\\:testHandleKernelRequestEvent\\(\\) has invalid typehint type Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseEvent\\.$#"
count: 1
path: test/EventListener/RequestListenerTest.php

-
message: "#^Property Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\RequestListenerTest\\:\\:\\$currentHub has no typehint specified\\.$#"
message: "#^Parameter \\#1 \\$event of method Sentry\\\\SentryBundle\\\\EventListener\\\\RequestListener\\:\\:handleKernelRequestEvent\\(\\) expects Sentry\\\\SentryBundle\\\\EventListener\\\\RequestListenerRequestEvent, Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseEvent\\|Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\RequestEvent given\\.$#"
count: 1
path: test/EventListener/RequestListenerTest.php

-
message: "#^Property Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\RequestListenerTest\\:\\:\\$options has no typehint specified\\.$#"
count: 1
message: "#^If condition is always true\\.$#"
count: 2
path: test/EventListener/RequestListenerTest.php

-
message: "#^Method Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\RequestListenerTest\\:\\:testOnKernelRequestUserDataIsSetToScope\\(\\) has parameter \\$user with no typehint specified\\.$#"
count: 1
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 2
path: test/EventListener/RequestListenerTest.php

-
message: "#^Method Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\RequestListenerTest\\:\\:userDataProvider\\(\\) return type has no value type specified in iterable type Generator\\.$#"
count: 1
message: "#^If condition is always false\\.$#"
count: 2
path: test/EventListener/RequestListenerTest.php

-
message: "#^Method Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\RequestListenerTest\\:\\:getTagsContext\\(\\) return type has no value type specified in iterable type array\\.$#"
message: "#^Parameter \\$controllerEvent of method Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\RequestListenerTest\\:\\:testHandleKernelControllerEvent\\(\\) has invalid typehint type Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\FilterControllerEvent\\.$#"
count: 1
path: test/EventListener/RequestListenerTest.php

-
message: "#^Method Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\RequestListenerTest\\:\\:createControllerEvent\\(\\) has no return typehint specified\\.$#"
message: "#^Parameter \\#1 \\$event of method Sentry\\\\SentryBundle\\\\EventListener\\\\RequestListener\\:\\:handleKernelControllerEvent\\(\\) expects Sentry\\\\SentryBundle\\\\EventListener\\\\RequestListenerControllerEvent, Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\FilterControllerEvent\\|Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\ControllerEvent given\\.$#"
count: 1
path: test/EventListener/RequestListenerTest.php

-
message: "#^Property Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\UserWithInterface\\:\\:\\$username has no typehint specified\\.$#"
message: "#^Parameter \\$event of method Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\SubRequestListenerTest\\:\\:testHandleKernelRequestEvent\\(\\) has invalid typehint type Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseEvent\\.$#"
count: 1
path: test/EventListener/RequestListenerTest.php
path: test/EventListener/SubRequestListenerTest.php

-
message: "#^Method Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\UserWithInterface\\:\\:eraseCredentials\\(\\) has no return typehint specified\\.$#"
message: "#^Call to method isMasterRequest\\(\\) on an unknown class Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseEvent\\.$#"
count: 1
path: test/EventListener/RequestListenerTest.php
path: test/EventListener/SubRequestListenerTest.php

-
message: "#^Property Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\ToStringUser\\:\\:\\$username has no typehint specified\\.$#"
message: "#^Parameter \\#1 \\$event of method Sentry\\\\SentryBundle\\\\EventListener\\\\SubRequestListener\\:\\:handleKernelRequestEvent\\(\\) expects Sentry\\\\SentryBundle\\\\EventListener\\\\SubRequestListenerRequestEvent, Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseEvent\\|Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\RequestEvent given\\.$#"
count: 1
path: test/EventListener/RequestListenerTest.php
path: test/EventListener/SubRequestListenerTest.php

-
message: "#^If condition is always false\\.$#"
count: 1
path: test/EventListener/SubRequestListenerTest.php

-
message: "#^If condition is always true\\.$#"
count: 1
path: test/EventListener/SubRequestListenerTest.php

-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 1
path: test/EventListener/SubRequestListenerTest.php

7 changes: 3 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
includes:
- phpstan-baseline.neon

parameters:
level: 7
paths:
- src/
- test/
ignoreErrors:
- '/Symfony\\Component\\HttpKernel\\Event\\(GetResponse|FilterController)Event not found.$/'
includes:
- phpstan-baseline.neon
25 changes: 0 additions & 25 deletions src/DependencyInjection/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ErrorHandler\Error\FatalError;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* This is the class that loads and manages your bundle configuration
Expand Down Expand Up @@ -157,11 +155,7 @@ private function configureErrorListener(ContainerBuilder $container, array $proc
{
if (! $processedConfiguration['register_error_listener']) {
$container->removeDefinition(ErrorListener::class);

return;
}

$this->tagExceptionListener($container);
}

private function configureMessengerListener(ContainerBuilder $container, array $processedConfiguration): void
Expand All @@ -175,25 +169,6 @@ private function configureMessengerListener(ContainerBuilder $container, array $
$container->getDefinition(MessengerListener::class)->setArgument(1, $processedConfiguration['capture_soft_fails']);
}

/**
* BC layer for Symfony < 4.3
*/
private function tagExceptionListener(ContainerBuilder $container): void
{
$listener = $container->getDefinition(ErrorListener::class);
$method = class_exists(ExceptionEvent::class) && method_exists(ExceptionEvent::class, 'getThrowable')
? 'onException'
: 'onKernelException';

$tagAttributes = [
'event' => KernelEvents::EXCEPTION,
'method' => $method,
'priority' => '%sentry.listener_priorities.request_error%',
];

$listener->addTag('kernel.event_listener', $tagAttributes);
}

private function configureMonologHandler(ContainerBuilder $container, array $monologConfiguration): void
{
$errorHandler = $monologConfiguration['error_handler'];
Expand Down
49 changes: 49 additions & 0 deletions src/EventListener/ConsoleCommandListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Sentry\SentryBundle\EventListener;

use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Symfony\Component\Console\Event\ConsoleErrorEvent;

/**
* This listener handles all errors thrown while running a console command and
* logs them to Sentry.
*/
final class ConsoleCommandListener
{
/**
* @var HubInterface The current hub
*/
private $hub;

/**
* Constructor.
*
* @param HubInterface $hub The current hub
*/
public function __construct(HubInterface $hub)
{
$this->hub = $hub;
}

/**
* Handles an error that happened while running a console command.
*
* @param ConsoleErrorEvent $event The event
*/
public function handleConsoleErrorEvent(ConsoleErrorEvent $event): void
{
$this->hub->withScope(function (Scope $scope) use ($event): void {
$command = $event->getCommand();

if (null !== $command && null !== $command->getName()) {
$scope->setTag('console.command', $command->getName());
}

$scope->setTag('console.command.exit_code', (string) $event->getExitCode());

$this->hub->captureException($event->getError());
});
}
}

0 comments on commit 26fb461

Please sign in to comment.