diff --git a/README.md b/README.md index 6f8322d0..99f3fd59 100644 --- a/README.md +++ b/README.md @@ -112,3 +112,12 @@ sentry: skip_capture: - "Symfony\\Component\\HttpKernel\\Exception\\HttpExceptionInterface" ``` + +### error types + +Define which error types should be reported. + +```yaml +sentry: + error_types: E_ALL & ~E_DEPRECATED & ~E_NOTICE +``` diff --git a/src/Sentry/SentryBundle/DependencyInjection/Configuration.php b/src/Sentry/SentryBundle/DependencyInjection/Configuration.php index 6dd072df..c866ab54 100644 --- a/src/Sentry/SentryBundle/DependencyInjection/Configuration.php +++ b/src/Sentry/SentryBundle/DependencyInjection/Configuration.php @@ -34,6 +34,9 @@ public function getConfigTreeBuilder() ->scalarNode('dsn') ->defaultNull() ->end() + ->scalarNode('error_types') + ->defaultNull() + ->end() ->scalarNode('exception_listener') ->defaultValue('Sentry\SentryBundle\EventListener\ExceptionListener') ->end() diff --git a/src/Sentry/SentryBundle/ErrorTypesParser.php b/src/Sentry/SentryBundle/ErrorTypesParser.php new file mode 100644 index 00000000..e2a0f8a3 --- /dev/null +++ b/src/Sentry/SentryBundle/ErrorTypesParser.php @@ -0,0 +1,73 @@ +expression = $expression; + } + + /** + * Parse and compute the error types expression + * + * @return int the parsed expression + */ + public function parse() + { + // convert constants to ints + $this->expression = $this->convertErrorConstants($this->expression); + $this->expression = str_replace( + array(",", " "), + array(".", ""), + $this->expression + ); + // remove anything which could be a security issue + $this->expression = preg_replace("/[^\d.+*%^|&~<>\/()-]/", "", $this->expression); + + return $this->compute($this->expression); + } + + + /** + * Converts error constants from string to int. + * + * @param string $expression e.g. E_ALL & ~E_DEPRECATED & ~E_NOTICE + * @return string convertes expression e.g. 32767 & ~8192 & ~8 + */ + private function convertErrorConstants($expression) + { + $output = preg_replace_callback("/(E_[a-zA-Z_]+)/", function ($errorConstant) { + if (defined($errorConstant[1])) { + return constant($errorConstant[1]); + } + return $errorConstant[0]; + }, $expression); + + return $output; + } + + /** + * Let PHP compute the prepared expression for us. + * + * @param string $expression prepared expression e.g. 32767&~8192&~8 + * @return int computed expression e.g. 24567 + */ + private function compute($expression) + { + $compute = create_function("", "return " . $expression . ";"); + + return 0 + $compute(); + } +} diff --git a/src/Sentry/SentryBundle/Resources/config/services.yml b/src/Sentry/SentryBundle/Resources/config/services.yml index 469d52ef..41980f86 100644 --- a/src/Sentry/SentryBundle/Resources/config/services.yml +++ b/src/Sentry/SentryBundle/Resources/config/services.yml @@ -1,7 +1,7 @@ services: sentry.client: class: '%sentry.client%' - arguments: ['%sentry.dsn%'] + arguments: ['%sentry.dsn%', {'error_types': '%sentry.error_types%'}] calls: - [setRelease, ['%sentry.release%']] - [setEnvironment, ['%sentry.environment%']] diff --git a/src/Sentry/SentryBundle/SentrySymfonyClient.php b/src/Sentry/SentryBundle/SentrySymfonyClient.php index f4a5e6ae..d77a0e3e 100644 --- a/src/Sentry/SentryBundle/SentrySymfonyClient.php +++ b/src/Sentry/SentryBundle/SentrySymfonyClient.php @@ -6,6 +6,11 @@ class SentrySymfonyClient extends \Raven_Client { public function __construct($dsn=null, $options=array()) { + if (array_key_exists('error_types', $options)) { + $exParser = new ErrorTypesParser($options['error_types']); + $options['error_types'] = $exParser->parse(); + } + $options['sdk'] = array( 'name' => 'sentry-symfony', 'version' => SentryBundle::VERSION, diff --git a/test/Sentry/SentryBundle/Test/ErrorTypesParserTest.php b/test/Sentry/SentryBundle/Test/ErrorTypesParserTest.php new file mode 100644 index 00000000..e6ea8017 --- /dev/null +++ b/test/Sentry/SentryBundle/Test/ErrorTypesParserTest.php @@ -0,0 +1,14 @@ +assertEquals($ex->parse(), E_ALL & ~E_DEPRECATED & ~E_NOTICE); + } +}