Skip to content
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

Do not capture E_ERROR type as fatal error #514

Merged
merged 4 commits into from
Nov 9, 2017

Conversation

stayallive
Copy link
Collaborator

@stayallive stayallive commented Nov 8, 2017

This should fix #408, can you guys please test it and and let me know if this is the way we should handle this or an alternative.

IMO this gist is that we should ignore E_ERROR exceptions as fatal in PHP 7.0+ since they can be captured by userland and are thus going through the regular exception handler before entering the fatal error handler thus creating duplicated exceptions.

This PR as a side effect also unifies the way the PHP version is checked.

@stayallive stayallive self-assigned this Nov 8, 2017
@stayallive stayallive requested review from ste93cry and Jean85 November 8, 2017 10:54
@stayallive stayallive force-pushed the prevent-duplicated-php7-exceptions branch from 935a063 to 0baca06 Compare November 8, 2017 10:55
@Jean85
Copy link
Collaborator

Jean85 commented Nov 8, 2017

I'll test this ASAP!

Maybe we can add a thing to this PR: update the PHPDoc of the Client::captureException() method to accept \Throwable|\Exception $exception here:

/**
* Log an exception to sentry
*
* @param Exception $exception The Exception object.
* @param array $data Additional attributes to pass with this event (see Sentry docs).
* @param mixed $logger
* @param mixed $vars
* @return string|null
*/
public function captureException($exception, $data = null, $logger = null, $vars = null)

What do you think?

@Jean85
Copy link
Collaborator

Jean85 commented Nov 8, 2017

Also: maybe we should add a (unit) test?

@stayallive
Copy link
Collaborator Author

I think that's a good idea.

For reference: why not remove \Throwable (this was my first thought)? This is because \Throwable was introduced in PHP 7.0 so \Exception remains for PHP < 7.0.

@stayallive
Copy link
Collaborator Author

@Jean85 I changed the test to check if the output of the shouldCaptureFatalError is correct. But not actually checking if this works correctly, any idea on how to test this? Cannot just call an undefined method since that does not seem to trigger this (tried adding a failing test before).

Copy link
Collaborator

@Jean85 Jean85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from my single comment, I tested this and it works! Great! 👍

// Do not capture E_ERROR since those can be caught by userland since PHP 7.0
// E_ERROR should already be handled by the exception handler
// This prevents duplicated exceptions in PHP 7.0+
if (version_compare(phpversion(), '7.0', '>=') && $type === E_ERROR) {
Copy link
Collaborator

@Jean85 Jean85 Nov 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if (PHP_VERSION_ID >= 70000) is faster? That constant is available since PHP 5.2, so that's not an issue to use it.

Copy link
Collaborator Author

@stayallive stayallive Nov 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like my PhpStorm inspections thought the same way. I changed it everywhere to stay consistent (updating the tests as we speak).

@Jean85
Copy link
Collaborator

Jean85 commented Nov 8, 2017

Sorry, I missed the changed test, I think it's enough for now!

@stayallive stayallive force-pushed the prevent-duplicated-php7-exceptions branch from 9ad8ba1 to 4913c1f Compare November 9, 2017 08:55
@stayallive stayallive merged commit 4f27a46 into master Nov 9, 2017
@stayallive stayallive deleted the prevent-duplicated-php7-exceptions branch November 9, 2017 09:07
Jean85 added a commit that referenced this pull request Nov 9, 2017
Add informations about #514
@Jean85 Jean85 mentioned this pull request Nov 9, 2017
Jean85 added a commit that referenced this pull request Nov 9, 2017
Add informations about #514
@mfb
Copy link
Contributor

mfb commented Feb 17, 2018

What are you all doing to capture fatal PHP errors, such as allowed memory size exhausted? To capture this fatal error successfully, I had to comment out the return FALSE; in shouldCaptureFatalError():

if (PHP_VERSION_ID >= 70000 && $type === E_ERROR) {
  // return false;
}

sample code:

<?php
include '../vendor/autoload.php';
$client = new Raven_Client('...');
$client->install();
while (TRUE) {
  $a[] = 11111111111;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Duplicate sentry issues
3 participants