Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 857ee17

Browse files
committedDec 11, 2022
Add error handler integrations option
1 parent 510dd81 commit 857ee17

File tree

6 files changed

+126
-11
lines changed

6 files changed

+126
-11
lines changed
 

‎CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- feat: Add `error_handler_integrations` option (#1439)
6+
57
## 3.12.0 (2022-11-22)
68

79
- feat: Add `before_send_transaction` option (#1424)

‎phpstan-baseline.neon

+5
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ parameters:
240240
count: 1
241241
path: src/Options.php
242242

243+
-
244+
message: "#^Method Sentry\\\\Options\\:\\:hasErrorHandlerIntegrations\\(\\) should return bool but returns mixed\\.$#"
245+
count: 1
246+
path: src/Options.php
247+
243248
-
244249
message: "#^Method Sentry\\\\Options\\:\\:isCompressionEnabled\\(\\) should return bool but returns mixed\\.$#"
245250
count: 1

‎src/Integration/IntegrationRegistry.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,19 @@ private function getIntegrationsToSetup(Options $options): array
122122
*/
123123
private function getDefaultIntegrations(Options $options): array
124124
{
125-
if (!$options->hasDefaultIntegrations()) {
126-
return [];
127-
}
125+
$integrations = [];
128126

129-
$integrations = [
130-
new RequestIntegration(),
131-
new TransactionIntegration(),
132-
new FrameContextifierIntegration(),
133-
new EnvironmentIntegration(),
134-
new ModulesIntegration(),
135-
];
127+
if ($options->hasDefaultIntegrations()) {
128+
$integrations = [
129+
new RequestIntegration(),
130+
new TransactionIntegration(),
131+
new FrameContextifierIntegration(),
132+
new EnvironmentIntegration(),
133+
new ModulesIntegration(),
134+
];
135+
}
136136

137-
if (null !== $options->getDsn()) {
137+
if ($options->hasErrorHandlerIntegrations() && null !== $options->getDsn()) {
138138
array_unshift($integrations, new ExceptionListenerIntegration(), new ErrorListenerIntegration(), new FatalErrorListenerIntegration());
139139
}
140140

‎src/Options.php

+24
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,28 @@ public function setDefaultIntegrations(bool $enable): void
608608
$this->options = $this->resolver->resolve($options);
609609
}
610610

611+
/**
612+
* Returns whether the error handler integrations are enabled or fallbacks
613+
* to whether the default integrations are enabled when null.
614+
*/
615+
public function hasErrorHandlerIntegrations(): bool
616+
{
617+
return $this->options['error_handler_integrations'] ?? $this->hasDefaultIntegrations();
618+
}
619+
620+
/**
621+
* Sets whether the error handler integrations are enabled.
622+
*
623+
* @param ?bool $enable Flag indicating whether the error handler integrations should be enabled, fallbacks
624+
* to "default_integrations" when null
625+
*/
626+
public function setErrorHandlerIntegrations(?bool $enable): void
627+
{
628+
$options = array_merge($this->options, ['error_handler_integrations' => $enable]);
629+
630+
$this->options = $this->resolver->resolve($options);
631+
}
632+
611633
/**
612634
* Gets the max length for values in the event payload.
613635
*/
@@ -811,6 +833,7 @@ private function configureOptions(OptionsResolver $resolver): void
811833
$resolver->setDefaults([
812834
'integrations' => [],
813835
'default_integrations' => true,
836+
'error_handler_integrations' => null,
814837
'send_attempts' => 0,
815838
'prefixes' => array_filter(explode(\PATH_SEPARATOR, get_include_path() ?: '')),
816839
'sample_rate' => 1,
@@ -874,6 +897,7 @@ private function configureOptions(OptionsResolver $resolver): void
874897
$resolver->setAllowedTypes('integrations', ['Sentry\\Integration\\IntegrationInterface[]', 'callable']);
875898
$resolver->setAllowedTypes('send_default_pii', 'bool');
876899
$resolver->setAllowedTypes('default_integrations', 'bool');
900+
$resolver->setAllowedTypes('error_handler_integrations', ['bool', 'null']);
877901
$resolver->setAllowedTypes('max_value_length', 'int');
878902
$resolver->setAllowedTypes('http_proxy', ['null', 'string']);
879903
$resolver->setAllowedTypes('http_connect_timeout', ['int', 'float']);

‎tests/Integration/IntegrationRegistryTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,46 @@ public function setupOnce(): void
298298
ModulesIntegration::class => new ModulesIntegration(),
299299
],
300300
];
301+
302+
yield 'Default integrations and no error handler integrations' => [
303+
new Options([
304+
'dsn' => 'http://public@example.com/sentry/1',
305+
'default_integrations' => true,
306+
'error_handler_integrations' => false,
307+
]),
308+
[
309+
'The "Sentry\\Integration\\RequestIntegration" integration has been installed.',
310+
'The "Sentry\\Integration\\TransactionIntegration" integration has been installed.',
311+
'The "Sentry\\Integration\\FrameContextifierIntegration" integration has been installed.',
312+
'The "Sentry\\Integration\\EnvironmentIntegration" integration has been installed.',
313+
'The "Sentry\\Integration\\ModulesIntegration" integration has been installed.',
314+
],
315+
[
316+
RequestIntegration::class => new RequestIntegration(),
317+
TransactionIntegration::class => new TransactionIntegration(),
318+
FrameContextifierIntegration::class => new FrameContextifierIntegration(),
319+
EnvironmentIntegration::class => new EnvironmentIntegration(),
320+
ModulesIntegration::class => new ModulesIntegration(),
321+
],
322+
];
323+
324+
yield 'No default integrations and error handler integrations' => [
325+
new Options([
326+
'dsn' => 'http://public@example.com/sentry/1',
327+
'default_integrations' => false,
328+
'error_handler_integrations' => true,
329+
]),
330+
[
331+
'The "Sentry\\Integration\\ExceptionListenerIntegration" integration has been installed.',
332+
'The "Sentry\\Integration\\ErrorListenerIntegration" integration has been installed.',
333+
'The "Sentry\\Integration\\FatalErrorListenerIntegration" integration has been installed.',
334+
],
335+
[
336+
ExceptionListenerIntegration::class => new ExceptionListenerIntegration(),
337+
ErrorListenerIntegration::class => new ErrorListenerIntegration(),
338+
FatalErrorListenerIntegration::class => new FatalErrorListenerIntegration(),
339+
],
340+
];
301341
}
302342

303343
/**

‎tests/OptionsTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,50 @@ static function (): void {},
364364
null,
365365
null,
366366
];
367+
368+
yield [
369+
'default_integrations',
370+
false,
371+
'hasDefaultIntegrations',
372+
'setDefaultIntegrations',
373+
null,
374+
null,
375+
];
376+
377+
yield [
378+
'error_handler_integrations',
379+
true,
380+
'hasErrorHandlerIntegrations',
381+
'setErrorHandlerIntegrations',
382+
null,
383+
null,
384+
];
385+
}
386+
387+
/**
388+
* @dataProvider errorHandlerIntegrationsOptionDataProvider
389+
*/
390+
public function testErrorHandlerIntegrationsFallback(
391+
bool $defaultIntegrationsValue,
392+
?bool $errorHandlerIntegrationsValue,
393+
bool $expectedValue
394+
): void {
395+
$options = new Options([
396+
'default_integrations' => $defaultIntegrationsValue,
397+
'error_handler_integrations' => $errorHandlerIntegrationsValue,
398+
]);
399+
400+
$this->assertEquals($expectedValue, $options->hasErrorHandlerIntegrations());
401+
}
402+
403+
public function errorHandlerIntegrationsOptionDataProvider(): iterable
404+
{
405+
yield [true, null, true];
406+
yield [true, false, false];
407+
yield [true, true, true];
408+
yield [false, null, false];
409+
yield [false, false, false];
410+
yield [false, true, true];
367411
}
368412

369413
/**

0 commit comments

Comments
 (0)
Please sign in to comment.