Skip to content

Commit 62fae71

Browse files
Jean85kefzce
authored andcommitted
Enable error handlers back (getsentry#322)
* 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
1 parent 25cf96e commit 62fae71

File tree

8 files changed

+80
-63
lines changed

8 files changed

+80
-63
lines changed

phpstan-baseline.neon

-5
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,6 @@ parameters:
230230
count: 1
231231
path: test/End2End/App/Kernel.php
232232

233-
-
234-
message: "#^Class PHPUnit_Framework_TestCase not found\\.$#"
235-
count: 1
236-
path: test/End2End/End2EndTest.php
237-
238233
-
239234
message: "#^Class Symfony\\\\Bundle\\\\FrameworkBundle\\\\Client not found\\.$#"
240235
count: 1

src/DependencyInjection/IntegrationFilterFactory.php

-35
This file was deleted.

src/DependencyInjection/SentryExtension.php

+8-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Exception\LogicException;
1818
use Symfony\Component\DependencyInjection\Loader;
1919
use Symfony\Component\DependencyInjection\Reference;
20+
use Symfony\Component\ErrorHandler\Error\FatalError;
2021
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2122
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
2223
use Symfony\Component\HttpKernel\KernelEvents;
@@ -128,18 +129,15 @@ private function passConfigurationToOptions(ContainerBuilder $container, array $
128129
}
129130
}
130131

131-
if (\array_key_exists('excluded_exceptions', $processedOptions) && $processedOptions['excluded_exceptions']) {
132-
$ignoreOptions = [
133-
'ignore_exceptions' => $processedOptions['excluded_exceptions'],
134-
];
135-
136-
$integrations[] = new Definition(IgnoreErrorsIntegration::class, [$ignoreOptions]);
137-
}
132+
// we ignore fatal errors wrapped by Symfony because they produce double event reporting
133+
$processedOptions['excluded_exceptions'][] = FatalError::class;
134+
$ignoreOptions = [
135+
'ignore_exceptions' => $processedOptions['excluded_exceptions'],
136+
];
138137

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

142-
$options->addMethodCall('setIntegrations', [$integrationsCallable]);
140+
$options->addMethodCall('setIntegrations', [$integrations]);
143141
}
144142

145143
/**

test/DependencyInjection/SentryExtensionTest.php

-10
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use Sentry\Breadcrumb;
99
use Sentry\ClientInterface;
1010
use Sentry\Event;
11-
use Sentry\Integration\ErrorListenerIntegration;
12-
use Sentry\Integration\ExceptionListenerIntegration;
1311
use Sentry\Integration\IntegrationInterface;
1412
use Sentry\Monolog\Handler;
1513
use Sentry\Options;
@@ -339,14 +337,6 @@ public function testIntegrations(): void
339337

340338
$found = false;
341339
foreach ($integrations as $integration) {
342-
if ($integration instanceof ErrorListenerIntegration) {
343-
$this->fail('Should not have ErrorListenerIntegration registered');
344-
}
345-
346-
if ($integration instanceof ExceptionListenerIntegration) {
347-
$this->fail('Should not have ExceptionListenerIntegration registered');
348-
}
349-
350340
if ($integration instanceof IntegrationMock) {
351341
$found = true;
352342
}

test/End2End/App/Controller/MainController.php

+14
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,27 @@ public function exception(): Response
3131
throw new \RuntimeException('This is an intentional error');
3232
}
3333

34+
public function fatal(): Response
35+
{
36+
$foo = eval("return new class() implements \Serializable {};");
37+
38+
return new Response('This response should not happen: ' . json_encode($foo));
39+
}
40+
3441
public function index(): Response
3542
{
3643
$this->sentry->captureMessage('Hello there');
3744

3845
return new Response('Hello there');
3946
}
4047

48+
public function notice(): Response
49+
{
50+
@trigger_error('This is an intentional notice', E_USER_NOTICE);
51+
52+
return new Response('Hello there');
53+
}
54+
4155
public function subrequest(): Response
4256
{
4357
$request = $this->requestStack->getCurrentRequest();

test/End2End/App/config.yml

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
sentry:
2+
options:
3+
capture_silenced_errors: true
4+
error_types: E_ALL & ~E_USER_DEPRECATED
5+
16
framework:
27
router: { resource: "%routing_config_dir%/routing.yml" }
38
secret: secret
@@ -8,6 +13,17 @@ services:
813
alias: Sentry\State\HubInterface
914
public: true
1015

16+
Sentry\ClientBuilderInterface:
17+
class: Sentry\ClientBuilder
18+
arguments:
19+
- '@Sentry\Options'
20+
calls:
21+
- method: setTransportFactory
22+
arguments:
23+
- '@Sentry\SentryBundle\Test\End2End\StubTransportFactory'
24+
25+
Sentry\SentryBundle\Test\End2End\StubTransportFactory: ~
26+
1127
Sentry\SentryBundle\Test\End2End\App\Controller\MainController:
1228
autowire: true
1329
tags:

test/End2End/StubTransportFactory.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Sentry\SentryBundle\Test\End2End;
4+
5+
use Sentry\Event;
6+
use Sentry\Options;
7+
use Sentry\Transport\TransportFactoryInterface;
8+
use Sentry\Transport\TransportInterface;
9+
10+
class StubTransportFactory implements TransportFactoryInterface
11+
{
12+
public const SEPARATOR = '###';
13+
14+
public function create(Options $options): TransportInterface
15+
{
16+
return new class() implements TransportInterface {
17+
public function send(Event $event): ?string
18+
{
19+
touch(End2EndTest::SENT_EVENTS_LOG);
20+
21+
if ($event->getMessage()) {
22+
$message = $event->getMessage();
23+
} elseif ($event->getExceptions()) {
24+
$message = $event->getExceptions()[0]['value'];
25+
} else {
26+
$message = 'NO MESSAGE NOR EXCEPTIONS';
27+
}
28+
29+
file_put_contents(
30+
End2EndTest::SENT_EVENTS_LOG,
31+
$event->getId() . ': ' . $message . PHP_EOL . StubTransportFactory::SEPARATOR . PHP_EOL,
32+
FILE_APPEND
33+
);
34+
35+
return $event->getId();
36+
}
37+
};
38+
}
39+
}

test/SentryBundleTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,16 @@ public function testContainerHasTestCommandRegisteredCorrectly(): void
127127
$this->assertArrayHasKey('console.command', $consoleListener->getTags());
128128
}
129129

130-
public function testIntegrationsListenersAreDisabledByDefault(): void
130+
public function testIntegrationsListenersAreEnabled(): void
131131
{
132132
$container = $this->getContainer();
133133

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

136136
$this->assertInstanceOf(HubInterface::class, $hub);
137137
$this->assertInstanceOf(IntegrationInterface::class, $hub->getIntegration(RequestIntegration::class));
138-
$this->assertNull($hub->getIntegration(ErrorListenerIntegration::class));
139-
$this->assertNull($hub->getIntegration(ExceptionListenerIntegration::class));
138+
$this->assertNotNull($hub->getIntegration(ErrorListenerIntegration::class));
139+
$this->assertNotNull($hub->getIntegration(ExceptionListenerIntegration::class));
140140
}
141141

142142
private function getContainer(): ContainerBuilder

0 commit comments

Comments
 (0)