Skip to content

Commit

Permalink
Unified error handlers. Now the regular error handler will receive th…
Browse files Browse the repository at this point in the history
…e fatal errors too.
  • Loading branch information
jrbasso committed Apr 14, 2012
1 parent a72288c commit 9beaa96
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
3 changes: 0 additions & 3 deletions app/Config/core.php
Expand Up @@ -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.
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions lib/Cake/Console/Templates/skel/Config/core.php
Expand Up @@ -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.
Expand All @@ -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
Expand Down
9 changes: 3 additions & 6 deletions lib/Cake/Core/App.php
Expand Up @@ -913,14 +913,11 @@ protected static function _checkFatalError() {
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());
}

}
19 changes: 13 additions & 6 deletions lib/Cake/Error/ErrorHandler.php
Expand Up @@ -158,6 +158,9 @@ public static function handleError($code, $description, $file = null, $line = nu
}
$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) {
Expand Down Expand Up @@ -186,24 +189,28 @@ public static function handleError($code, $description, $file = null, $line = nu
/**
* 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;
}

/**
Expand Down
9 changes: 3 additions & 6 deletions lib/Cake/Test/Case/Error/ErrorHandlerTest.php
Expand Up @@ -89,7 +89,6 @@ public static function errorProvider() {
return array(
array(E_USER_NOTICE, 'Notice'),
array(E_USER_WARNING, 'Warning'),
array(E_USER_ERROR, 'Fatal Error'),
);
}

Expand Down Expand Up @@ -248,19 +247,18 @@ public function testHandleFatalErrorPage() {

$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.');
$this->assertContains((string)$line, $result, 'line missing.');

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.');
Expand All @@ -280,10 +278,9 @@ public function testHandleFatalErrorLog() {
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');
Expand Down

0 comments on commit 9beaa96

Please sign in to comment.