Skip to content

Commit 235aab6

Browse files
authored
Merge pull request #34 from d3f3kt/master
Add error types option
2 parents 1447190 + b2b0ad3 commit 235aab6

File tree

6 files changed

+105
-1
lines changed

6 files changed

+105
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,12 @@ sentry:
112112
skip_capture:
113113
- "Symfony\\Component\\HttpKernel\\Exception\\HttpExceptionInterface"
114114
```
115+
116+
### error types
117+
118+
Define which error types should be reported.
119+
120+
```yaml
121+
sentry:
122+
error_types: E_ALL & ~E_DEPRECATED & ~E_NOTICE
123+
```

src/Sentry/SentryBundle/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public function getConfigTreeBuilder()
3434
->scalarNode('dsn')
3535
->defaultNull()
3636
->end()
37+
->scalarNode('error_types')
38+
->defaultNull()
39+
->end()
3740
->scalarNode('exception_listener')
3841
->defaultValue('Sentry\SentryBundle\EventListener\ExceptionListener')
3942
->end()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Sentry\SentryBundle;
4+
5+
/**
6+
* Evaluate an error types expression.
7+
*/
8+
class ErrorTypesParser
9+
{
10+
private $expression = null;
11+
12+
/**
13+
* Initialize ErrorParser
14+
*
15+
* @param string $expression Error Types e.g. E_ALL & ~E_DEPRECATED & ~E_NOTICE
16+
*/
17+
public function __construct($expression)
18+
{
19+
$this->expression = $expression;
20+
}
21+
22+
/**
23+
* Parse and compute the error types expression
24+
*
25+
* @return int the parsed expression
26+
*/
27+
public function parse()
28+
{
29+
// convert constants to ints
30+
$this->expression = $this->convertErrorConstants($this->expression);
31+
$this->expression = str_replace(
32+
array(",", " "),
33+
array(".", ""),
34+
$this->expression
35+
);
36+
// remove anything which could be a security issue
37+
$this->expression = preg_replace("/[^\d.+*%^|&~<>\/()-]/", "", $this->expression);
38+
39+
return $this->compute($this->expression);
40+
}
41+
42+
43+
/**
44+
* Converts error constants from string to int.
45+
*
46+
* @param string $expression e.g. E_ALL & ~E_DEPRECATED & ~E_NOTICE
47+
* @return string convertes expression e.g. 32767 & ~8192 & ~8
48+
*/
49+
private function convertErrorConstants($expression)
50+
{
51+
$output = preg_replace_callback("/(E_[a-zA-Z_]+)/", function ($errorConstant) {
52+
if (defined($errorConstant[1])) {
53+
return constant($errorConstant[1]);
54+
}
55+
return $errorConstant[0];
56+
}, $expression);
57+
58+
return $output;
59+
}
60+
61+
/**
62+
* Let PHP compute the prepared expression for us.
63+
*
64+
* @param string $expression prepared expression e.g. 32767&~8192&~8
65+
* @return int computed expression e.g. 24567
66+
*/
67+
private function compute($expression)
68+
{
69+
$compute = create_function("", "return " . $expression . ";");
70+
71+
return 0 + $compute();
72+
}
73+
}

src/Sentry/SentryBundle/Resources/config/services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
services:
22
sentry.client:
33
class: '%sentry.client%'
4-
arguments: ['%sentry.dsn%']
4+
arguments: ['%sentry.dsn%', {'error_types': '%sentry.error_types%'}]
55
calls:
66
- [setRelease, ['%sentry.release%']]
77
- [setEnvironment, ['%sentry.environment%']]

src/Sentry/SentryBundle/SentrySymfonyClient.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ class SentrySymfonyClient extends \Raven_Client
66
{
77
public function __construct($dsn=null, $options=array())
88
{
9+
if (array_key_exists('error_types', $options)) {
10+
$exParser = new ErrorTypesParser($options['error_types']);
11+
$options['error_types'] = $exParser->parse();
12+
}
13+
914
$options['sdk'] = array(
1015
'name' => 'sentry-symfony',
1116
'version' => SentryBundle::VERSION,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Sentry\SentryBundle\Test;
4+
5+
use Sentry\SentryBundle\ErrorTypesParser;
6+
7+
class ErrorTypesParserTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function test_error_types_parser()
10+
{
11+
$ex = new ErrorTypesParser('E_ALL & ~E_DEPRECATED & ~E_NOTICE');
12+
$this->assertEquals($ex->parse(), E_ALL & ~E_DEPRECATED & ~E_NOTICE);
13+
}
14+
}

0 commit comments

Comments
 (0)