Skip to content

Commit

Permalink
fixes getsentry#552 Capturing E_ERROR fatal errors broken on PHP 7
Browse files Browse the repository at this point in the history
ignore "Uncaught" if exception handling is active; capture other errors
  • Loading branch information
mfb committed Feb 20, 2018
1 parent 4dd0a3e commit 17233a7
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions lib/Raven/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class Raven_ErrorHandler
*/
protected $error_types = null;

/**
* @var bool
* Used to prevent duplicate exceptions in PHP 7.0+.
*/
protected $exception_handled;

public function __construct($client, $send_errors_last = false, $error_types = null,
$__error_types = null)
{
Expand All @@ -76,6 +82,11 @@ public function bitwiseOr($a, $b)
public function handleException($e, $isError = false, $vars = null)
{
$e->event_id = $this->client->captureException($e, null, null, $vars);
// Indicate that exception handling is enabled so we can ignore Uncaught Error
// fatal errors in PHP 7.0+.
if (!$isError) {
$this->exception_handled = true;
}

if (!$isError && $this->call_existing_exception_handler) {
if ($this->old_exception_handler !== null) {
Expand Down Expand Up @@ -129,7 +140,7 @@ public function handleFatalError()
return;
}

if ($this->shouldCaptureFatalError($error['type'])) {
if ($this->shouldCaptureFatalError($error['type'], $error['message'])) {
$e = new ErrorException(
@$error['message'], 0, @$error['type'],
@$error['file'], @$error['line']
Expand All @@ -138,12 +149,15 @@ public function handleFatalError()
}
}

public function shouldCaptureFatalError($type)
public function shouldCaptureFatalError($type, $message)
{
// 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 (PHP_VERSION_ID >= 70000 && $type === E_ERROR) {
// If exception handling is enabled, do not capture Uncaught Error fatal errors.
// These should have already been handled as exceptions. This prevents
// duplicate exceptions in PHP 7.0+.
if (PHP_VERSION_ID >= 70000 && $type === E_ERROR
&& $this->exception_handled
&& strpos($message, 'Uncaught') === 0
) {
return false;
}

Expand Down

0 comments on commit 17233a7

Please sign in to comment.