mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Unified error handlers. Now the regular error handler will receive the fatal errors too.
This commit is contained in:
parent
a72288c378
commit
9beaa9602f
5 changed files with 19 additions and 24 deletions
|
@ -41,8 +41,6 @@
|
|||
*
|
||||
* Options:
|
||||
*
|
||||
* - `fatalErrorHandler` - callback - The callback to handle fatal errors. You can set this to any
|
||||
* callable type, including anonymous functions. Setting it to false will disable the feature.
|
||||
* - `handler` - callback - The callback to handle errors. You can set this to any callable type,
|
||||
* including anonymous functions.
|
||||
* - `level` - int - The level of errors you are interested in capturing.
|
||||
|
@ -51,7 +49,6 @@
|
|||
* @see ErrorHandler for more information on error handling and configuration.
|
||||
*/
|
||||
Configure::write('Error', array(
|
||||
'fatalErrorHandler' => 'ErrorHandler::handleFatalError',
|
||||
'handler' => 'ErrorHandler::handleError',
|
||||
'level' => E_ALL & ~E_DEPRECATED,
|
||||
'trace' => true
|
||||
|
|
|
@ -41,8 +41,6 @@
|
|||
*
|
||||
* Options:
|
||||
*
|
||||
* - `fatalErrorHandler` - callback - The callback to handle fatal errors. You can set this to any
|
||||
* callable type, including anonymous functions. Setting it to false will disable the feature.
|
||||
* - `handler` - callback - The callback to handle errors. You can set this to any callable type,
|
||||
* including anonymous functions.
|
||||
* - `level` - int - The level of errors you are interested in capturing.
|
||||
|
@ -51,7 +49,6 @@
|
|||
* @see ErrorHandler for more information on error handling and configuration.
|
||||
*/
|
||||
Configure::write('Error', array(
|
||||
'fatalErrorHandler' => 'ErrorHandler::handleFatalError',
|
||||
'handler' => 'ErrorHandler::handleError',
|
||||
'level' => E_ALL & ~E_DEPRECATED,
|
||||
'trace' => true
|
||||
|
|
|
@ -913,14 +913,11 @@ class App {
|
|||
return;
|
||||
}
|
||||
|
||||
$fatalErrorHandler = Configure::read('Error.fatalErrorHandler');
|
||||
if ($fatalErrorHandler === null) {
|
||||
$fatalErrorHandler = 'ErrorHandler::handleFatalError';
|
||||
}
|
||||
if (!is_callable($fatalErrorHandler)) {
|
||||
$errorHandler = Configure::read('Error.handler');
|
||||
if (!is_callable($errorHandler)) {
|
||||
return;
|
||||
}
|
||||
call_user_func($fatalErrorHandler, $lastError);
|
||||
call_user_func($errorHandler, $lastError['type'], $lastError['message'], $lastError['file'], $lastError['line'], array());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -158,6 +158,9 @@ class ErrorHandler {
|
|||
}
|
||||
$errorConfig = Configure::read('Error');
|
||||
list($error, $log) = self::mapErrorCode($code);
|
||||
if ($log === LOG_ERROR) {
|
||||
return self::handleFatalError($code, $description, $file, $line);
|
||||
}
|
||||
|
||||
$debug = Configure::read('debug');
|
||||
if ($debug) {
|
||||
|
@ -186,24 +189,28 @@ class ErrorHandler {
|
|||
/**
|
||||
* Generate an error page when some fatal error happens.
|
||||
*
|
||||
* @param array $error Array with error information. See `error_get_last()` function
|
||||
* @return void
|
||||
* @param integer $code Code of error
|
||||
* @param string $description Error description
|
||||
* @param string $file File on which error occurred
|
||||
* @param integer $line Line that triggered the error
|
||||
* @return boolean
|
||||
*/
|
||||
public static function handleFatalError($error) {
|
||||
$logMessage = 'Fatal Error (' . $error['type'] . '): ' . $error['message'] . ' in [' . $error['file'] . ', line ' . $error['line'] . ']';
|
||||
public static function handleFatalError($code, $description, $file, $line) {
|
||||
$logMessage = 'Fatal Error (' . $code . '): ' . $description . ' in [' . $file . ', line ' . $line . ']';
|
||||
CakeLog::write(LOG_ERROR, $logMessage);
|
||||
|
||||
$exceptionHandler = Configure::read('Exception.handler');
|
||||
if (!is_callable($exceptionHandler)) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
ob_clean();
|
||||
if (Configure::read('debug')) {
|
||||
call_user_func($exceptionHandler, new FatalErrorException($error['message'], 500, $error['file'], $error['line']));
|
||||
call_user_func($exceptionHandler, new FatalErrorException($description, 500, $file, $line));
|
||||
} else {
|
||||
call_user_func($exceptionHandler, new InternalErrorException());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -89,7 +89,6 @@ class ErrorHandlerTest extends CakeTestCase {
|
|||
return array(
|
||||
array(E_USER_NOTICE, 'Notice'),
|
||||
array(E_USER_WARNING, 'Warning'),
|
||||
array(E_USER_ERROR, 'Fatal Error'),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -248,11 +247,10 @@ class ErrorHandlerTest extends CakeTestCase {
|
|||
|
||||
$originalDebugLevel = Configure::read('debug');
|
||||
$line = __LINE__;
|
||||
$error = array('type' => E_ERROR, 'message' => 'Something wrong', 'file' => __FILE__, 'line' => $line);
|
||||
|
||||
ob_start();
|
||||
Configure::write('debug', 1);
|
||||
ErrorHandler::handleFatalError($error);
|
||||
ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
|
||||
$result = ob_get_clean();
|
||||
$this->assertContains('Something wrong', $result, 'message missing.');
|
||||
$this->assertContains(__FILE__, $result, 'filename missing.');
|
||||
|
@ -260,7 +258,7 @@ class ErrorHandlerTest extends CakeTestCase {
|
|||
|
||||
ob_start();
|
||||
Configure::write('debug', 0);
|
||||
ErrorHandler::handleFatalError($error);
|
||||
ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
|
||||
$result = ob_get_clean();
|
||||
$this->assertNotContains('Something wrong', $result, 'message must not appear.');
|
||||
$this->assertNotContains(__FILE__, $result, 'filename must not appear.');
|
||||
|
@ -280,10 +278,9 @@ class ErrorHandlerTest extends CakeTestCase {
|
|||
if (file_exists(LOGS . 'error.log')) {
|
||||
unlink(LOGS . 'error.log');
|
||||
}
|
||||
$error = array('type' => E_ERROR, 'message' => 'Something wrong', 'file' => __FILE__, 'line' => __LINE__);
|
||||
|
||||
ob_start();
|
||||
ErrorHandler::handleFatalError($error);
|
||||
ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, __LINE__);
|
||||
ob_clean();
|
||||
|
||||
$log = file(LOGS . 'error.log');
|
||||
|
|
Loading…
Reference in a new issue