From 29da61ee9960e4555dd7a9e6d48312533a631e18 Mon Sep 17 00:00:00 2001 From: David Geistert Date: Thu, 3 Nov 2016 19:59:41 +0100 Subject: [PATCH 1/2] Add error types parser This parser converts an error types expression from string to integer. e.g. E_ALL & ~E_DEPRECATED & ~E_NOTICE -> 24567 --- src/Sentry/SentryBundle/ErrorTypesParser.php | 69 +++++++++++++++++++ .../Test/ErrorTypesParserTest.php | 14 ++++ 2 files changed, 83 insertions(+) create mode 100644 src/Sentry/SentryBundle/ErrorTypesParser.php create mode 100644 test/Sentry/SentryBundle/Test/ErrorTypesParserTest.php diff --git a/src/Sentry/SentryBundle/ErrorTypesParser.php b/src/Sentry/SentryBundle/ErrorTypesParser.php new file mode 100644 index 00000000..4174a2c8 --- /dev/null +++ b/src/Sentry/SentryBundle/ErrorTypesParser.php @@ -0,0 +1,69 @@ +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([",", " "], [".", ""], $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/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); + } +} From b2b0ad35ebe50c6f636682e1e9dfb8709998831b Mon Sep 17 00:00:00 2001 From: David Geistert Date: Thu, 3 Nov 2016 20:03:21 +0100 Subject: [PATCH 2/2] Add error types option According to my pull request: https://github.com/getsentry/sentry-php/pull/369 With this error types options its possible to configure which error types should be reported. --- README.md | 9 +++++++++ .../SentryBundle/DependencyInjection/Configuration.php | 3 +++ src/Sentry/SentryBundle/ErrorTypesParser.php | 8 ++++++-- src/Sentry/SentryBundle/Resources/config/services.yml | 2 +- src/Sentry/SentryBundle/SentrySymfonyClient.php | 5 +++++ 5 files changed, 24 insertions(+), 3 deletions(-) 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 index 4174a2c8..e2a0f8a3 100644 --- a/src/Sentry/SentryBundle/ErrorTypesParser.php +++ b/src/Sentry/SentryBundle/ErrorTypesParser.php @@ -28,7 +28,11 @@ public function parse() { // convert constants to ints $this->expression = $this->convertErrorConstants($this->expression); - $this->expression = str_replace([",", " "], [".", ""], $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); @@ -56,7 +60,7 @@ private function convertErrorConstants($expression) /** * 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 */ 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,