Skip to content

Commit

Permalink
Adding Debugger output to ErrorHandler::handleError, and adding traci…
Browse files Browse the repository at this point in the history
…ng for log writes.

Renaming Debugger::_output() -> Debugger::outputError() and making it public instead of protected.
  • Loading branch information
markstory committed Nov 26, 2010
1 parent f373fc1 commit 722b1a0
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 9 deletions.
14 changes: 8 additions & 6 deletions cake/libs/debugger.php
Expand Up @@ -581,11 +581,12 @@ public function output($format = null, $strings = array()) {
}

/**
* Renders error messages
* Takes a processed array of data from an error and displays it in the chosen format.
*
* @param array $data Data about the current error
* @param string $data
* @return void
*/
protected function _output($data = array()) {
public function outputError($data) {
$defaults = array(
'level' => 0,
'error' => 0,
Expand All @@ -594,13 +595,14 @@ protected function _output($data = array()) {
'description' => '',
'file' => '',
'line' => 0,
'context' => array()
'context' => array(),
'start' => 2
);
$data += $defaults;

$files = $this->trace(array('start' => 2, 'format' => 'points'));
$files = $this->trace(array('start' => $data['start'], 'format' => 'points'));
$code = $this->excerpt($files[0]['file'], $files[0]['line'] - 1, 1);
$trace = $this->trace(array('start' => 2, 'depth' => '20'));
$trace = $this->trace(array('start' => $data['start'], 'depth' => '20'));
$insertOpts = array('before' => '{:', 'after' => '}');
$context = array();
$links = array();
Expand Down
69 changes: 67 additions & 2 deletions cake/libs/error_handler.php
Expand Up @@ -184,6 +184,7 @@ public static function handleException(Exception $exception) {
* will log errors to CakeLog, when debug == 0.
*
* You can use Configure::write('Error.level', $value); to set what type of errors will be handled here.
* Stack traces for errors can be enabled with Configure::write('Error.trace', true);
*
* @param integer $code Code of error
* @param string $description Error description
Expand All @@ -193,18 +194,82 @@ public static function handleException(Exception $exception) {
* @return boolean true if error was handled
*/
public static function handleError($code, $description, $file = null, $line = null, $context = null) {
$errorConfig = Configure::read('Error');
if ($errorConfig['level'] && ($code & ~$errorConfig['level'])) {
return;
}
list($error, $log) = self::_mapErrorCode($code);

$debug = Configure::read('debug');
if ($debug) {
if (!class_exists('Debugger')) {
require LIBS . 'debugger.php';
}
return Debugger::showError($code, $description, $file, $line, $context);
$data = array(
'level' => $log,
'code' => $code,
'error' => $error,
'description' => $description,
'file' => $file,
'line' => $line,
'context' => $context,
'start' => 2,
'path' => Debugger::trimPath($file)
);
return Debugger::getInstance()->outputError($data);
} else {
if (!class_exists('CakeLog')) {
require LIBS . 'cake_log.php';
}
return CakeLog::logError($code, $description, $file, $line, $context);
$message = $error . ' (' . $code . '): ' . $description . ' in [' . $file . ', line ' . $line . ']';
if (!empty($errorConfig['trace'])) {
if (!class_exists('Debugger')) {
require LIBS . 'debugger.php';
}
$trace = Debugger::trace(array('start' => 1, 'format' => 'log'));
$message .= "\nTrace:\n" . $trace . "\n";
}
return CakeLog::write($log, $message);
}
}

/**
* Map an error code into an Error word, and log location.
*
* @param int $code Error code to map
* @return array Array of error word, and log location.
*/
protected static function _mapErrorCode($code) {
switch ($code) {
case E_PARSE:
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
$error = 'Fatal Error';
$log = LOG_ERROR;
break;
case E_WARNING:
case E_USER_WARNING:
case E_COMPILE_WARNING:
case E_RECOVERABLE_ERROR:
$error = 'Warning';
$log = LOG_WARNING;
break;
case E_NOTICE:
case E_USER_NOTICE:
$error = 'Notice';
$log = LOG_NOTICE;
break;
case E_STRICT:
$error = 'Strict';
$log = LOG_NOTICE;
break;
default:
return array();
break;
}
return array($error, $log);
}

/**
Expand Down
35 changes: 34 additions & 1 deletion cake/tests/cases/libs/error_handler.test.php
Expand Up @@ -170,6 +170,8 @@ function setUp() {
$request->base = '';
Router::setRequestInfo($request);
$this->_debug = Configure::read('debug');
$this->_error = Configure::read('Error');
Configure::write('debug', 2);
}

/**
Expand All @@ -179,6 +181,7 @@ function setUp() {
*/
function teardown() {
Configure::write('debug', $this->_debug);
Configure::write('Error', $this->_error);
App::build();
if ($this->_restoreError) {
restore_error_handler();
Expand Down Expand Up @@ -220,7 +223,10 @@ function testHandleErrorDebugOn() {
*/
function testHandleErrorDebugOff() {
Configure::write('debug', 0);
@unlink(LOGS . 'debug.log');
Configure::write('Error.trace', false);
if (file_exists(LOGS . 'debug.log')) {
@unlink(LOGS . 'debug.log');
}

set_error_handler('ErrorHandler::handleError');
$this->_restoreError = true;
Expand All @@ -236,6 +242,33 @@ function testHandleErrorDebugOff() {
@unlink(LOGS . 'debug.log');
}

/**
* Test that errors going into CakeLog include traces.
*
* @return void
*/
function testHandleErrorLoggingTrace() {
Configure::write('debug', 0);
Configure::write('Error.trace', true);
if (file_exists(LOGS . 'debug.log')) {
@unlink(LOGS . 'debug.log');
}

set_error_handler('ErrorHandler::handleError');
$this->_restoreError = true;

$out .= '';

$result = file(LOGS . 'debug.log');
$this->assertPattern(
'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} Notice: Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
$result[0]
);
$this->assertPattern('/^Trace:/', $result[1]);
$this->assertPattern('/^ErrorHandlerTest\:\:testHandleErrorLoggingTrace\(\)/', $result[2]);
@unlink(LOGS . 'debug.log');
}

/**
* test handleException generating a page.
*
Expand Down

0 comments on commit 722b1a0

Please sign in to comment.