Skip to content

Commit

Permalink
Start moving ConsoleErrorHandler to use new base class.
Browse files Browse the repository at this point in the history
Update code and tests for ConsoleErrorHandler to work with new template
method and base class.
  • Loading branch information
markstory committed Aug 22, 2013
1 parent aceb08e commit fda5907
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 44 deletions.
74 changes: 33 additions & 41 deletions lib/Cake/Console/ConsoleErrorHandler.php
Expand Up @@ -15,13 +15,15 @@
namespace Cake\Console;

use Cake\Core\Configure;
use Cake\Error\FatalErrorException;
use Cake\Error\BaseErrorHandler;
use Cake\Error\ErrorHandler;

/**
* Error Handler for Cake console. Does simple printing of the
* exception that occurred and the stack trace of the error.
*/
class ConsoleErrorHandler {
class ConsoleErrorHandler extends BaseErrorHandler {

/**
* Standard error stream.
Expand Down Expand Up @@ -50,62 +52,52 @@ public function __construct($options = []) {
$this->_options = $options;
}

/**
* Register the error and exception handlers.
*
* @return void
*/
public function register() {
$level = -1;
if (isset($this->_options['errorLevel'])) {
$level = $this->_options['errorLevel'];
}
error_reporting($level);
set_error_handler([$this, 'handleError'], $level);
set_exception_handler([$this, 'handleException']);
}

/**
* Handle a exception in the console environment. Prints a message to stderr.
*
* @param Exception $exception The exception to handle
* @return integer Exit code from exception caught.
*/
public function handleException(\Exception $exception) {
$this->_stderr->write(__d('cake_console', "<error>Error:</error> %s\n%s",
$errorName = __d('cake_console', 'Error:');
if ($exception instanceof FatalErrorException) {
$errorName = __d('cake_console', 'Fatal Error:');
}
$message = sprintf(
"<error>%s</error> %s in [%s, line %s]\n%s",
$errorName,
$exception->getMessage(),
$exception->getFile(),
$exception->getLine(),
$exception->getTraceAsString()
));
);
$this->_stderr->write($message);
return $exception->getCode() ?: 1;
}

protected function _displayException($exception) {
}

/**
* Handle errors in the console environment. Writes errors to stderr,
* and logs messages if Configure::read('debug') is 0.
* Prints an error to stderr.
*
* @param integer $code Error code
* @param string $description Description of the error.
* @param string $file The file the error occurred in.
* @param integer $line The line the error occurred on.
* @param array $context The backtrace of the error.
* Template method of BaseErrorHandler.
*
* @param array $error An array of error data.
* @param boolean $debug Whether or not the app is in debug mode.
* @return void
*/
public function handleError($code, $description, $file = null, $line = null, $context = null) {
if (error_reporting() === 0) {
return;
}
list($name, $log) = ErrorHandler::mapErrorCode($code);
$message = __d('cake_console', '%s in [%s, line %s]', $description, $file, $line);
$this->_stderr->write(__d('cake_console', "<error>%s Error:</error> %s\n", $name, $message));

if (!Configure::read('debug')) {
Log::write($log, $message);
}

if ($log === LOG_ERR) {
// @todo define how to handle exit
return 1;
}
protected function _displayError($error, $debug) {
$message = __d('cake_console', '%s in [%s, line %s]',
$error['description'],
$error['file'],
$error['line']
);
$message = __d('cake_console', "<error>%s Error:</error> %s\n",
$error['error'],
$message
);
$this->_stderr->write($message);
}

}
31 changes: 31 additions & 0 deletions lib/Cake/Error/BaseErrorHandler.php
Expand Up @@ -92,6 +92,37 @@ public function handleError($code, $description, $file = null, $line = null, $co
$this->_logError($log, $data);
}

/**
* Display/Log a fatal error.
*
* @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 function handleFatalError($code, $description, $file, $line) {
$data = [
'code' => $code,
'description' => $description,
'file' => $file,
'line' => $line,
'error' => 'Fatal Error',
];
$this->_logError(LOG_ERR, $data);

if (ob_get_level()) {
ob_end_clean();
}

if (Configure::read('debug')) {
$this->handleException(new FatalErrorException($description, 500, $file, $line));
} else {
$this->handleException(new InternalErrorException());
}
return false;
}

/**
* Log an error.
*
Expand Down
8 changes: 5 additions & 3 deletions lib/Cake/Test/TestCase/Console/ConsoleErrorHandlerTest.php
Expand Up @@ -65,9 +65,10 @@ public function testHandleError() {
* @return void
*/
public function testHandleFatalError() {
$content = "<error>Fatal Error Error:</error> This is a fatal error in [/some/file, line 275]\n";
ob_start();
$content = "<error>Fatal Error:</error> This is a fatal error in [/some/file, line 275]\n";
$this->stderr->expects($this->once())->method('write')
->with($content);
->with($this->stringContains($content));

$this->Error->handleError(E_USER_ERROR, 'This is a fatal error', '/some/file', 275);
}
Expand All @@ -79,8 +80,9 @@ public function testHandleFatalError() {
*/
public function testCakeErrors() {
$exception = new Error\MissingActionException('Missing action');
$message = sprintf('Missing action in [%s, line %s]', $exception->getFile(), $exception->getLine());
$this->stderr->expects($this->once())->method('write')
->with($this->stringContains('Missing action'));
->with($this->stringContains($message));

$result = $this->Error->handleException($exception);
$this->assertEquals(404, $result);
Expand Down

0 comments on commit fda5907

Please sign in to comment.