Skip to content

Commit

Permalink
Enable error handlers back (getsentry#322)
Browse files Browse the repository at this point in the history
* Reinforce E2E tests with log of sent events

* Add failing test for notices

* Fix wrong setup in test

* Improve E2E tests; add case for fatals

* Try to revert the integration disabling to have the full error reporting back

* Fix test after last modification

* Require --dev symfony/process for client isolation

* Do not capture deprecations in E2E tests

* Fix CS

* Fix PHPStan

* Fix last PHPStan error

* Remove unneeded alias

* Remove unused class

* Try to avoid double reporting of fatal errors

* Add changelog entry

* Fix after-merge issues
  • Loading branch information
Jean85 authored and kefzce committed Sep 21, 2020
1 parent 25cf96e commit 62fae71
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 63 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Expand Up @@ -230,11 +230,6 @@ parameters:
count: 1
path: test/End2End/App/Kernel.php

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

-
message: "#^Class Symfony\\\\Bundle\\\\FrameworkBundle\\\\Client not found\\.$#"
count: 1
Expand Down
35 changes: 0 additions & 35 deletions src/DependencyInjection/IntegrationFilterFactory.php

This file was deleted.

18 changes: 8 additions & 10 deletions src/DependencyInjection/SentryExtension.php
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Loader;
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;
Expand Down Expand Up @@ -128,18 +129,15 @@ private function passConfigurationToOptions(ContainerBuilder $container, array $
}
}

if (\array_key_exists('excluded_exceptions', $processedOptions) && $processedOptions['excluded_exceptions']) {
$ignoreOptions = [
'ignore_exceptions' => $processedOptions['excluded_exceptions'],
];

$integrations[] = new Definition(IgnoreErrorsIntegration::class, [$ignoreOptions]);
}
// we ignore fatal errors wrapped by Symfony because they produce double event reporting
$processedOptions['excluded_exceptions'][] = FatalError::class;
$ignoreOptions = [
'ignore_exceptions' => $processedOptions['excluded_exceptions'],
];

$integrationsCallable = new Definition('callable', [$integrations]);
$integrationsCallable->setFactory([IntegrationFilterFactory::class, 'create']);
$integrations[] = new Definition(IgnoreErrorsIntegration::class, [$ignoreOptions]);

$options->addMethodCall('setIntegrations', [$integrationsCallable]);
$options->addMethodCall('setIntegrations', [$integrations]);
}

/**
Expand Down
10 changes: 0 additions & 10 deletions test/DependencyInjection/SentryExtensionTest.php
Expand Up @@ -8,8 +8,6 @@
use Sentry\Breadcrumb;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\Integration\ErrorListenerIntegration;
use Sentry\Integration\ExceptionListenerIntegration;
use Sentry\Integration\IntegrationInterface;
use Sentry\Monolog\Handler;
use Sentry\Options;
Expand Down Expand Up @@ -339,14 +337,6 @@ public function testIntegrations(): void

$found = false;
foreach ($integrations as $integration) {
if ($integration instanceof ErrorListenerIntegration) {
$this->fail('Should not have ErrorListenerIntegration registered');
}

if ($integration instanceof ExceptionListenerIntegration) {
$this->fail('Should not have ExceptionListenerIntegration registered');
}

if ($integration instanceof IntegrationMock) {
$found = true;
}
Expand Down
14 changes: 14 additions & 0 deletions test/End2End/App/Controller/MainController.php
Expand Up @@ -31,13 +31,27 @@ public function exception(): Response
throw new \RuntimeException('This is an intentional error');
}

public function fatal(): Response
{
$foo = eval("return new class() implements \Serializable {};");

return new Response('This response should not happen: ' . json_encode($foo));
}

public function index(): Response
{
$this->sentry->captureMessage('Hello there');

return new Response('Hello there');
}

public function notice(): Response
{
@trigger_error('This is an intentional notice', E_USER_NOTICE);

return new Response('Hello there');
}

public function subrequest(): Response
{
$request = $this->requestStack->getCurrentRequest();
Expand Down
16 changes: 16 additions & 0 deletions test/End2End/App/config.yml
@@ -1,3 +1,8 @@
sentry:
options:
capture_silenced_errors: true
error_types: E_ALL & ~E_USER_DEPRECATED

framework:
router: { resource: "%routing_config_dir%/routing.yml" }
secret: secret
Expand All @@ -8,6 +13,17 @@ services:
alias: Sentry\State\HubInterface
public: true

Sentry\ClientBuilderInterface:
class: Sentry\ClientBuilder
arguments:
- '@Sentry\Options'
calls:
- method: setTransportFactory
arguments:
- '@Sentry\SentryBundle\Test\End2End\StubTransportFactory'

Sentry\SentryBundle\Test\End2End\StubTransportFactory: ~

Sentry\SentryBundle\Test\End2End\App\Controller\MainController:
autowire: true
tags:
Expand Down
39 changes: 39 additions & 0 deletions test/End2End/StubTransportFactory.php
@@ -0,0 +1,39 @@
<?php

namespace Sentry\SentryBundle\Test\End2End;

use Sentry\Event;
use Sentry\Options;
use Sentry\Transport\TransportFactoryInterface;
use Sentry\Transport\TransportInterface;

class StubTransportFactory implements TransportFactoryInterface
{
public const SEPARATOR = '###';

public function create(Options $options): TransportInterface
{
return new class() implements TransportInterface {
public function send(Event $event): ?string
{
touch(End2EndTest::SENT_EVENTS_LOG);

if ($event->getMessage()) {
$message = $event->getMessage();
} elseif ($event->getExceptions()) {
$message = $event->getExceptions()[0]['value'];
} else {
$message = 'NO MESSAGE NOR EXCEPTIONS';
}

file_put_contents(
End2EndTest::SENT_EVENTS_LOG,
$event->getId() . ': ' . $message . PHP_EOL . StubTransportFactory::SEPARATOR . PHP_EOL,
FILE_APPEND
);

return $event->getId();
}
};
}
}
6 changes: 3 additions & 3 deletions test/SentryBundleTest.php
Expand Up @@ -127,16 +127,16 @@ public function testContainerHasTestCommandRegisteredCorrectly(): void
$this->assertArrayHasKey('console.command', $consoleListener->getTags());
}

public function testIntegrationsListenersAreDisabledByDefault(): void
public function testIntegrationsListenersAreEnabled(): void
{
$container = $this->getContainer();

$hub = $container->get(HubInterface::class);

$this->assertInstanceOf(HubInterface::class, $hub);
$this->assertInstanceOf(IntegrationInterface::class, $hub->getIntegration(RequestIntegration::class));
$this->assertNull($hub->getIntegration(ErrorListenerIntegration::class));
$this->assertNull($hub->getIntegration(ExceptionListenerIntegration::class));
$this->assertNotNull($hub->getIntegration(ErrorListenerIntegration::class));
$this->assertNotNull($hub->getIntegration(ExceptionListenerIntegration::class));
}

private function getContainer(): ContainerBuilder
Expand Down

0 comments on commit 62fae71

Please sign in to comment.