Skip to content

Commit

Permalink
Merge pull request #34 from d3f3kt/master
Browse files Browse the repository at this point in the history
Add error types option
  • Loading branch information
dcramer authored Nov 17, 2016
2 parents 1447190 + b2b0ad3 commit 235aab6
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
3 changes: 3 additions & 0 deletions src/Sentry/SentryBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
73 changes: 73 additions & 0 deletions src/Sentry/SentryBundle/ErrorTypesParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Sentry\SentryBundle;

/**
* Evaluate an error types expression.
*/
class ErrorTypesParser
{
private $expression = null;

/**
* Initialize ErrorParser
*
* @param string $expression Error Types e.g. E_ALL & ~E_DEPRECATED & ~E_NOTICE
*/
public function __construct($expression)
{
$this->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();
}
}
2 changes: 1 addition & 1 deletion src/Sentry/SentryBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -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%']]
Expand Down
5 changes: 5 additions & 0 deletions src/Sentry/SentryBundle/SentrySymfonyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions test/Sentry/SentryBundle/Test/ErrorTypesParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Sentry\SentryBundle\Test;

use Sentry\SentryBundle\ErrorTypesParser;

class ErrorTypesParserTest extends \PHPUnit_Framework_TestCase
{
public function test_error_types_parser()
{
$ex = new ErrorTypesParser('E_ALL & ~E_DEPRECATED & ~E_NOTICE');
$this->assertEquals($ex->parse(), E_ALL & ~E_DEPRECATED & ~E_NOTICE);
}
}

0 comments on commit 235aab6

Please sign in to comment.