Skip to content

Commit

Permalink
Restore proper fatal error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Sep 20, 2016
1 parent 2108830 commit aaf4f82
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 55 deletions.
42 changes: 22 additions & 20 deletions lib/Raven/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ class Raven_ErrorHandler
private $call_existing_error_handler = false;
private $reservedMemory;
private $send_errors_last = false;
private $fatal_error_types = array(
E_ERROR,
E_PARSE,
E_CORE_ERROR,
E_CORE_WARNING,
E_COMPILE_ERROR,
E_COMPILE_WARNING,
E_STRICT,
);

/**
* @var array
Expand Down Expand Up @@ -68,6 +77,11 @@ public function handleException($e, $isError = false, $vars = null)

public function handleError($type, $message, $file = '', $line = 0, $context = array())
{
// https://php.net/set_error_handler
// The following error types cannot be handled with a user defined function: E_ERROR,
// E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and
// most of E_STRICT raised in the file where set_error_handler() is called.

// we always need to bind _last_handled_error in the case of a suppressed
// error getting passed to handleFatalError. In PHP 5.x it seems that
// ``error_get_last`` is not always populated, so we instead always bind
Expand Down Expand Up @@ -104,30 +118,18 @@ public function handleError($type, $message, $file = '', $line = 0, $context = a

public function handleFatalError()
{
unset($this->reservedMemory);

if (null === $error = error_get_last()) {
return;
}

unset($this->reservedMemory);

if (error_reporting() !== 0) {
$error_types = $this->error_types;
if ($error_types === null) {
$error_types = error_reporting();
}
if ($error_types & $error['type']) {
// ensure that if this error was reported via handleError that
// we don't duplicate it here
if ($this->_last_handled_error === $error) {
return;
}

$e = new ErrorException(
@$error['message'], 0, @$error['type'],
@$error['file'], @$error['line']
);
$this->handleException($e, true);
}
if ($error['type'] & $this->fatal_error_types) {
$e = new ErrorException(
@$error['message'], 0, @$error['type'],
@$error['file'], @$error['line']
);
$this->handleException($e, true);
}
}

Expand Down
35 changes: 0 additions & 35 deletions test/Raven/Tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,41 +157,6 @@ public function testErrorHandlerDefaultsErrorReporting()
trigger_error('Warning', E_USER_WARNING);
}

public function testHandleFatalError()
{
$client = $this->getMock('Client', array('captureException'));
$client->expects($this->once())
->method('captureException');

$handler = new Raven_ErrorHandler($client);

# https://php.net/manual/en/function.error-get-last.php#113518
set_error_handler('var_dump', 0);
@$undef_var;
restore_error_handler();

$handler->handleFatalError();
}

public function testHandleFatalErrorDuplicate()
{
$client = $this->getMock('Client', array('captureException'));
$client->expects($this->once())
->method('captureException');

$handler = new Raven_ErrorHandler($client);

# https://php.net/manual/en/function.error-get-last.php#113518
set_error_handler('var_dump', 0);
@$undef_var;
restore_error_handler();

$error = error_get_last();

$handler->handleError($error['type'], $error['message'], $error['file'], $error['line']);
$handler->handleFatalError();
}

public function testFluidInterface()
{
$client = $this->getMock('Client', array('captureException'));
Expand Down

0 comments on commit aaf4f82

Please sign in to comment.