Skip to content

Commit 965ca7b

Browse files
authored
Merge pull request #202 from getsentry/add-silenced-errors-option
Add silenced errors option
2 parents dc394e1 + 249d3fc commit 965ca7b

File tree

5 files changed

+114
-109
lines changed

5 files changed

+114
-109
lines changed

phpstan.neon

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ parameters:
55
- test/
66
ignoreErrors:
77
- "/Call to function method_exists.. with 'Symfony.+' and 'getProjectDir' will always evaluate to false./"
8-
excludes_analyse:
9-
- '%currentWorkingDirectory%/src/DependencyInjection/Configuration.php'
8+
- "/Call to function method_exists.. with 'Symfony.+' and 'getRootNode' will always evaluate to false./"
109

1110
includes:
1211
- vendor/jangregor/phpstan-prophecy/src/extension.neon

src/DependencyInjection/Configuration.php

Lines changed: 82 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Sentry\SentryBundle\DependencyInjection;
44

5+
use Jean85\PrettyVersions;
56
use Sentry\Options;
67
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
78
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
@@ -24,128 +25,106 @@ public function getConfigTreeBuilder()
2425
{
2526
$treeBuilder = new TreeBuilder('sentry');
2627
/** @var ArrayNodeDefinition $rootNode */
27-
$rootNode = \method_exists(TreeBuilder::class, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root('sentry');
28+
$rootNode = \method_exists(TreeBuilder::class, 'getRootNode')
29+
? $treeBuilder->getRootNode()
30+
: $treeBuilder->root('sentry');
2831

2932
// Basic Sentry configuration
3033
$rootNode->children()
3134
->scalarNode('dsn')
32-
->beforeNormalization()
33-
->ifString()
34-
->then($this->getTrimClosure())
35-
->end()
36-
->defaultNull()
37-
->end();
35+
->defaultNull()
36+
->beforeNormalization()
37+
->ifString()
38+
->then($this->getTrimClosure());
3839

3940
// Options array (to be passed to Sentry\Options constructor) -- please keep alphabetical order!
4041
$optionsNode = $rootNode->children()
4142
->arrayNode('options')
4243
->addDefaultsIfNotSet();
4344

4445
$defaultValues = new Options();
45-
46-
$optionsNode
47-
->children()
48-
->booleanNode('attach_stacktrace')->end()
49-
->variableNode('before_breadcrumb')
50-
->validate()
51-
->ifTrue($this->isNotAValidCallback())
52-
->thenInvalid('Expecting callable or service reference, got %s')
53-
->end()
54-
->end()
55-
->variableNode('before_send')
56-
->validate()
57-
->ifTrue($this->isNotAValidCallback())
58-
->thenInvalid('Expecting callable or service reference, got %s')
59-
->end()
60-
->end()
61-
->booleanNode('default_integrations')->end()
62-
->integerNode('context_lines')
63-
->min(0)
64-
->max(99)
65-
->end()
66-
->booleanNode('enable_compression')->end()
67-
->scalarNode('environment')
68-
->defaultValue('%kernel.environment%')
69-
->cannotBeEmpty()
70-
->end()
71-
->scalarNode('error_types')
72-
->end()
73-
->arrayNode('in_app_exclude')
74-
->defaultValue([
75-
'%kernel.cache_dir%',
76-
$this->getProjectRoot() . '/vendor',
77-
])
78-
->prototype('scalar')->end()
79-
->end()
80-
->arrayNode('excluded_exceptions')
81-
->defaultValue($defaultValues->getExcludedExceptions())
82-
->prototype('scalar')->end()
83-
->end()
84-
->scalarNode('http_proxy')
85-
->end()
86-
->arrayNode('integrations')
87-
->prototype('scalar')
88-
->validate()
89-
->ifTrue(function ($value): bool {
90-
if (! is_string($value)) {
91-
return true;
92-
}
93-
94-
return '@' !== substr($value, 0, 1);
95-
})
96-
->thenInvalid('Expecting service reference, got %s')
97-
->end()
98-
->end()
99-
->end()
100-
->scalarNode('logger')
101-
->end()
102-
->integerNode('max_breadcrumbs')
103-
->min(1)
104-
->end()
105-
->integerNode('max_value_length')
106-
->min(1)
107-
->end()
108-
->arrayNode('prefixes')
109-
->defaultValue($defaultValues->getPrefixes())
110-
->prototype('scalar')->end()
111-
->end()
112-
->scalarNode('project_root')
113-
->defaultValue($this->getProjectRoot())
114-
->end()
115-
->scalarNode('release')
116-
->end()
117-
->floatNode('sample_rate')
118-
->min(0.0)
119-
->max(1.0)
120-
->end()
121-
->integerNode('send_attempts')
122-
->min(1)
123-
->end()
124-
->booleanNode('send_default_pii')->end()
125-
->scalarNode('server_name')
126-
->end()
127-
->arrayNode('tags')
128-
->normalizeKeys(false)
129-
->prototype('scalar')
130-
->end()
131-
;
46+
$optionsChildNodes = $optionsNode->children();
47+
48+
$optionsChildNodes->booleanNode('attach_stacktrace');
49+
$optionsChildNodes->variableNode('before_breadcrumb')
50+
->validate()
51+
->ifTrue($this->isNotAValidCallback())
52+
->thenInvalid('Expecting callable or service reference, got %s');
53+
$optionsChildNodes->variableNode('before_send')
54+
->validate()
55+
->ifTrue($this->isNotAValidCallback())
56+
->thenInvalid('Expecting callable or service reference, got %s');
57+
if (PrettyVersions::getVersion('sentry/sentry')->getPrettyVersion() !== '2.0.0') {
58+
$optionsChildNodes->booleanNode('capture_silenced_errors');
59+
}
60+
$optionsChildNodes->integerNode('context_lines')
61+
->min(0)
62+
->max(99);
63+
$optionsChildNodes->booleanNode('default_integrations');
64+
$optionsChildNodes->booleanNode('enable_compression');
65+
$optionsChildNodes->scalarNode('environment')
66+
->defaultValue('%kernel.environment%')
67+
->cannotBeEmpty();
68+
$optionsChildNodes->scalarNode('error_types');
69+
$optionsChildNodes->arrayNode('in_app_exclude')
70+
->defaultValue([
71+
'%kernel.cache_dir%',
72+
$this->getProjectRoot() . '/vendor',
73+
])
74+
->prototype('scalar');
75+
$optionsChildNodes->arrayNode('excluded_exceptions')
76+
->defaultValue($defaultValues->getExcludedExceptions())
77+
->prototype('scalar');
78+
$optionsChildNodes->scalarNode('http_proxy');
79+
$optionsChildNodes->arrayNode('integrations')
80+
->prototype('scalar')
81+
->validate()
82+
->ifTrue(function ($value): bool {
83+
if (! is_string($value)) {
84+
return true;
85+
}
86+
87+
return '@' !== substr($value, 0, 1);
88+
})
89+
->thenInvalid('Expecting service reference, got %s');
90+
$optionsChildNodes->scalarNode('logger');
91+
$optionsChildNodes->integerNode('max_breadcrumbs')
92+
->min(1);
93+
$optionsChildNodes->integerNode('max_value_length')
94+
->min(1);
95+
$optionsChildNodes->arrayNode('prefixes')
96+
->defaultValue($defaultValues->getPrefixes())
97+
->prototype('scalar');
98+
$optionsChildNodes->scalarNode('project_root')
99+
->defaultValue($this->getProjectRoot());
100+
$optionsChildNodes->scalarNode('release');
101+
$optionsChildNodes->floatNode('sample_rate')
102+
->min(0.0)
103+
->max(1.0);
104+
$optionsChildNodes->integerNode('send_attempts')
105+
->min(1);
106+
$optionsChildNodes->booleanNode('send_default_pii');
107+
$optionsChildNodes->scalarNode('server_name');
108+
$optionsChildNodes->arrayNode('tags')
109+
->normalizeKeys(false)
110+
->prototype('scalar');
132111

133112
// Bundle-specific configuration
134-
$rootNode->children()
113+
$listenerPriorities = $rootNode->children()
135114
->arrayNode('listener_priorities')
136-
->addDefaultsIfNotSet()
137-
->children()
138-
->scalarNode('request')->defaultValue(1)->end()
139-
->scalarNode('console')->defaultValue(1)->end()
140-
->end()
141-
->end();
115+
->addDefaultsIfNotSet()
116+
->children();
117+
$listenerPriorities->scalarNode('request')
118+
->defaultValue(1);
119+
$listenerPriorities->scalarNode('console')
120+
->defaultValue(1);
142121

143122
return $treeBuilder;
144123
}
145124

146-
private function getTrimClosure(): callable
125+
private function getTrimClosure(): \Closure
147126
{
148-
return function ($str) {
127+
return function ($str): ?string {
149128
$value = trim($str);
150129
if ($value === '') {
151130
return null;

src/DependencyInjection/SentryExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ private function passConfigurationToOptions(ContainerBuilder $container, array $
5252
$processedOptions = $processedConfiguration['options'];
5353
$mappableOptions = [
5454
'attach_stacktrace',
55+
'capture_silenced_errors',
5556
'context_lines',
5657
'default_integrations',
5758
'enable_compression',

test/DependencyInjection/ConfigurationTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Sentry\SentryBundle\Test\DependencyInjection;
44

5+
use Jean85\PrettyVersions;
56
use PHPUnit\Framework\TestCase;
67
use Sentry\Options;
78
use Sentry\SentryBundle\DependencyInjection\Configuration;
@@ -18,8 +19,14 @@ public function testDataProviderIsMappingTheRightNumberOfOptions(): void
1819
$providerData = $this->optionValuesProvider();
1920
$supportedOptions = \array_unique(\array_column($providerData, 0));
2021

22+
$expectedCount = self::SUPPORTED_SENTRY_OPTIONS_COUNT;
23+
24+
if (PrettyVersions::getVersion('sentry/sentry')->getPrettyVersion() !== '2.0.0') {
25+
++$expectedCount;
26+
}
27+
2128
$this->assertCount(
22-
self::SUPPORTED_SENTRY_OPTIONS_COUNT,
29+
$expectedCount,
2330
$supportedOptions,
2431
'Provider for configuration options mismatch: ' . PHP_EOL . print_r($supportedOptions, true)
2532
);
@@ -83,7 +90,7 @@ public function testOptionValuesProcessing(string $option, $value): void
8390

8491
public function optionValuesProvider(): array
8592
{
86-
return [
93+
$options = [
8794
['attach_stacktrace', true],
8895
['before_breadcrumb', 'count'],
8996
['before_send', 'count'],
@@ -112,6 +119,12 @@ public function optionValuesProvider(): array
112119
['server_name', 'server001.example.com'],
113120
['tags', ['tag-name' => 'value']],
114121
];
122+
123+
if (PrettyVersions::getVersion('sentry/sentry')->getPrettyVersion() !== '2.0.0') {
124+
$options[] = ['capture_silenced_errors', true];
125+
}
126+
127+
return $options;
115128
}
116129

117130
/**

test/DependencyInjection/SentryExtensionTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Sentry\SentryBundle\Test\DependencyInjection;
44

5+
use Jean85\PrettyVersions;
56
use PHPUnit\Framework\TestCase;
67
use Sentry\Breadcrumb;
78
use Sentry\Event;
@@ -30,8 +31,14 @@ public function testDataProviderIsMappingTheRightNumberOfOptions(): void
3031
$supportedOptions = \array_unique(\array_column($providerData, 0));
3132

3233
// subtracted one is `integration`, which cannot be tested with the provider
34+
$expectedCount = ConfigurationTest::SUPPORTED_SENTRY_OPTIONS_COUNT - 1;
35+
36+
if (PrettyVersions::getVersion('sentry/sentry')->getPrettyVersion() !== '2.0.0') {
37+
++$expectedCount;
38+
}
39+
3340
$this->assertCount(
34-
ConfigurationTest::SUPPORTED_SENTRY_OPTIONS_COUNT - 1,
41+
$expectedCount,
3542
$supportedOptions,
3643
'Provider for configuration options mismatch: ' . PHP_EOL . print_r($supportedOptions, true)
3744
);
@@ -90,7 +97,7 @@ public function testValuesArePassedToOptions(string $name, $value, string $gette
9097

9198
public function optionsValueProvider(): array
9299
{
93-
return [
100+
$options = [
94101
['attach_stacktrace', true, 'shouldAttachStacktrace'],
95102
['before_breadcrumb', __NAMESPACE__ . '\mockBeforeBreadcrumb', 'getBeforeBreadcrumbCallback'],
96103
['before_send', __NAMESPACE__ . '\mockBeforeSend', 'getBeforeSendCallback'],
@@ -114,6 +121,12 @@ public function optionsValueProvider(): array
114121
['server_name', 'server.example.com'],
115122
['tags', ['tag-name' => 'tag-value']],
116123
];
124+
125+
if (PrettyVersions::getVersion('sentry/sentry')->getPrettyVersion() !== '2.0.0') {
126+
$options[] = ['capture_silenced_errors', true, 'shouldCaptureSilencedErrors'];
127+
}
128+
129+
return $options;
117130
}
118131

119132
public function testErrorTypesAreParsed(): void

0 commit comments

Comments
 (0)