Skip to content

Commit

Permalink
Fix PHP7 Throwable to be caught by low level try catch.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Nov 25, 2017
1 parent 5a76d37 commit c1f0b94
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Error/Debugger.php
Expand Up @@ -766,7 +766,7 @@ public static function addFormat($format, array $strings)
/**
* Takes a processed array of data from an error and displays it in the chosen format.
*
* @param string $data Data to output.
* @param array $data Data to output.
* @return void
*/
public function outputError($data)
Expand Down
35 changes: 26 additions & 9 deletions src/Error/ErrorHandler.php
Expand Up @@ -18,6 +18,7 @@

use Cake\Core\App;
use Exception;
use Throwable;

/**
* Error Handler provides basic error and exception handling for your application. It captures and
Expand Down Expand Up @@ -137,16 +138,10 @@ protected function _displayException($exception)
$response = $renderer->render();
$this->_clearOutput();
$this->_sendResponse($response);
} catch (Throwable $t) {
$this->_logInternalError($t);
} catch (Exception $e) {
// Disable trace for internal errors.
$this->_options['trace'] = false;
$message = sprintf(
"[%s] %s\n%s", // Keeping same message format
get_class($e),
$e->getMessage(),
$e->getTraceAsString()
);
trigger_error($message, E_USER_ERROR);
$this->_logInternalError($e);
}
}

Expand All @@ -164,6 +159,28 @@ protected function _clearOutput()
}
}

/**
* Log both PHP5 and PHP7 errors.
*
* The PHP5 part will be removed with 4.0.
*
* @param \Throwable|\Exception $exception
*
* @return void
*/
protected function _logInternalError($exception)
{
// Disable trace for internal errors.
$this->_options['trace'] = false;
$message = sprintf(
"[%s] %s\n%s", // Keeping same message format
get_class($exception),
$exception->getMessage(),
$exception->getTraceAsString()
);
trigger_error($message, E_USER_ERROR);
}

/**
* Method that can be easily stubbed in testing.
*
Expand Down

0 comments on commit c1f0b94

Please sign in to comment.