Skip to content

Commit

Permalink
Merge pull request #8518 from garethellis36/customize-logged-error-me…
Browse files Browse the repository at this point in the history
…ssage

[2.x] Extracts error trace in ErrorHandler to a helper function to allow customization
  • Loading branch information
markstory committed Mar 24, 2016
2 parents a0ea7a0 + a6702b7 commit 96c9521
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
46 changes: 30 additions & 16 deletions lib/Cake/Error/ErrorHandler.php
Expand Up @@ -207,7 +207,6 @@ public static function handleError($code, $description, $file = null, $line = nu
if (error_reporting() === 0) {
return false;
}
$errorConfig = Configure::read('Error');
list($error, $log) = static::mapErrorCode($code);
if ($log === LOG_ERR) {
return static::handleFatalError($code, $description, $file, $line);
Expand All @@ -228,21 +227,7 @@ public static function handleError($code, $description, $file = null, $line = nu
);
return Debugger::getInstance()->outputError($data);
}
$message = $error . ' (' . $code . '): ' . $description . ' in [' . $file . ', line ' . $line . ']';
if (!empty($errorConfig['trace'])) {
// https://bugs.php.net/bug.php?id=65322
if (version_compare(PHP_VERSION, '5.4.21', '<')) {
if (!class_exists('Debugger')) {
App::load('Debugger');
}
if (!class_exists('CakeText')) {
App::uses('CakeText', 'Utility');
App::load('CakeText');
}
}
$trace = Debugger::trace(array('start' => 1, 'format' => 'log'));
$message .= "\nTrace:\n" . $trace . "\n";
}
$message = static::_getErrorMessage($error, $code, $description, $file, $line);
return CakeLog::write($log, $message);
}

Expand Down Expand Up @@ -328,4 +313,33 @@ public static function mapErrorCode($code) {
return array($error, $log);
}

/**
* Generate the string to use to describe the error.
*
* @param string $error The error type (e.g. "Warning")
* @param int $code Code of error
* @param string $description Error description
* @param string $file File on which error occurred
* @param int $line Line that triggered the error
* @return string
*/
protected static function _getErrorMessage($error, $code, $description, $file, $line) {
$errorConfig = Configure::read('Error');
$message = $error . ' (' . $code . '): ' . $description . ' in [' . $file . ', line ' . $line . ']';
if (!empty($errorConfig['trace'])) {
// https://bugs.php.net/bug.php?id=65322
if (version_compare(PHP_VERSION, '5.4.21', '<')) {
if (!class_exists('Debugger')) {
App::load('Debugger');
}
if (!class_exists('CakeText')) {
App::uses('CakeText', 'Utility');
App::load('CakeText');
}
}
$trace = Debugger::trace(array('start' => 1, 'format' => 'log'));
$message .= "\nTrace:\n" . $trace . "\n";
}
return $message;
}
}
2 changes: 1 addition & 1 deletion lib/Cake/Test/Case/Error/ErrorHandlerTest.php
Expand Up @@ -203,7 +203,7 @@ public function testHandleErrorLoggingTrace() {
$result[0]
);
$this->assertRegExp('/^Trace:/', $result[1]);
$this->assertRegExp('/^ErrorHandlerTest\:\:testHandleErrorLoggingTrace\(\)/', $result[2]);
$this->assertRegExp('/^ErrorHandlerTest\:\:testHandleErrorLoggingTrace\(\)/', $result[3]);
if (file_exists(LOGS . 'debug.log')) {
unlink(LOGS . 'debug.log');
}
Expand Down

0 comments on commit 96c9521

Please sign in to comment.