-
Notifications
You must be signed in to change notification settings - Fork 179
Add error types option #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test cases seems lacking. For configuration extension, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah they def are |
||
|
||
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it not be simpler to just pass array's RavenClient expect instead of transforming strings to constants, back to Sentry levels again?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you're right - 'errro_types' was a typo. It's corrected now.
Of course it would be easier, but this is the established syntax to define error levels in php.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a way (legacy one, I might add) on how you define error reporting levels for PHP interpreter - nothing more. This configuration however is meant for Sentry, and Sentry has it's own error levels defined already - so it only makes sense, and is in a way expected behavior to follow Sentry convention.
As far as I know, otherwise, it's inconsistent - Symfony client uses one set of levels, Sentry uses another. What gives?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really know which Sentry error levels do you mean. The Raven_ErrorHandler exactly needs the error levels which i defined it in the configuration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, terribly sorry, I was under impression RavenClient uses "error", "warning" kind of strings.
Apart from that - can we avoid at least part of the conversions? For example, instead of having the php.ini-like single variable string
E_ALL && ~E_WARNING
, perhaps we can have array of levels we want and then just get the values via constant? Without all the preg_replace etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, consider that this code will be run every time there is an issue - so we probably want to keep the logic there to the absolute minimum. There aren't that many error types, and perhaps it should be inverse: e.g. list the types you don't want to receive. That would be consistent with exception filtering.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That code is not rocket science. It's a simply preg_replace to secure the php eval function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course. However, consider that this code is used in projects of all sizes, and this code is executed on construct - which is both good (executed just once) and bad thing (executed always). It's not a big issue if you get few hundred thousand executions per day. It becomes an issue when you have this running couple of billion times a day (like for us) - especially if there is a better and much less costly way to do it. Converting a list of "exclude" rules to constants and passing them on to Raven is such a way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't make a mountain out of a mole hill. It's a simply string replace operation.
And furthermore the code gets only executed if you have defined error types in your configuration.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short update - I've built a small test case for you:
With following results. The complete init takes 2ms and the parser takes 0ms. That should be enough to prove a string replace function is not expensive.