Skip to content

Commit

Permalink
Make console error handler no longer a static class.
Browse files Browse the repository at this point in the history
Not using static means there are fewer global symbols and
configuration/use will eventually be simpler.
  • Loading branch information
markstory committed Aug 20, 2013
1 parent e64f864 commit 0889ae5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
49 changes: 34 additions & 15 deletions lib/Cake/Console/ConsoleErrorHandler.php
Expand Up @@ -20,8 +20,6 @@
/**
* Error Handler for Cake console. Does simple printing of the
* exception that occurred and the stack trace of the error.
*
* @package Cake.Console
*/
class ConsoleErrorHandler {

Expand All @@ -30,18 +28,41 @@ class ConsoleErrorHandler {
*
* @var ConsoleOutput
*/
public static $stderr;
protected $_stderr;

/**
* Options for this instance.
*
* @var array
*/
protected $_options;

/**
* Constructor
*
* @param array $options Options for the error handler.
*/
public function __construct($options = []) {
if (empty($options['stderr'])) {
$options['stderr'] = new ConsoleOutput('php://stderr');
}
$this->_stderr = $options['stderr'];
$this->_options = $options;
}

/**
* Get the stderr object for the console error handling.
* Register the error and exception handlers
*
* @return ConsoleOutput
* @return void
*/
public static function getStderr() {
if (empty(static::$stderr)) {
static::$stderr = new ConsoleOutput('php://stderr');
public function register() {
$level = -1;
if (isset($this->_options['errorLevel'])) {
$level = $this->_options['errorLevel'];
}
return static::$stderr;
error_reporting($level);
set_error_handler([$this, 'handleError'], $level);
set_exception_handler([$this, 'handleException']);
}

/**
Expand All @@ -50,9 +71,8 @@ public static function getStderr() {
* @param Exception $exception The exception to handle
* @return integer Exit code from exception caught.
*/
public static function handleException(\Exception $exception) {
$stderr = static::getStderr();
$stderr->write(__d('cake_console', "<error>Error:</error> %s\n%s",
public function handleException(\Exception $exception) {
$this->_stderr->write(__d('cake_console', "<error>Error:</error> %s\n%s",
$exception->getMessage(),
$exception->getTraceAsString()
));
Expand All @@ -70,14 +90,13 @@ public static function handleException(\Exception $exception) {
* @param array $context The backtrace of the error.
* @return void
*/
public static function handleError($code, $description, $file = null, $line = null, $context = null) {
public function handleError($code, $description, $file = null, $line = null, $context = null) {
if (error_reporting() === 0) {
return;
}
$stderr = static::getStderr();
list($name, $log) = ErrorHandler::mapErrorCode($code);
$message = __d('cake_console', '%s in [%s, line %s]', $description, $file, $line);
$stderr->write(__d('cake_console', "<error>%s Error:</error> %s\n", $name, $message));
$this->_stderr->write(__d('cake_console', "<error>%s Error:</error> %s\n", $name, $message));

if (!Configure::read('debug')) {
Log::write($log, $message);
Expand Down
16 changes: 8 additions & 8 deletions lib/Cake/Test/TestCase/Console/ConsoleErrorHandlerTest.php
Expand Up @@ -36,8 +36,8 @@ class ConsoleErrorHandlerTest extends TestCase {
*/
public function setUp() {
parent::setUp();
$this->Error = $this->getMock('Cake\Console\ConsoleErrorHandler', ['_stop']);
ConsoleErrorHandler::$stderr = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
$this->stderr = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
$this->Error = new ConsoleErrorHandler(['stderr' => $this->stderr]);
}

/**
Expand All @@ -57,7 +57,7 @@ public function tearDown() {
*/
public function testHandleError() {
$content = "<error>Notice Error:</error> This is a notice error in [/some/file, line 275]\n";
ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
$this->stderr->expects($this->once())->method('write')
->with($content);

$this->Error->handleError(E_NOTICE, 'This is a notice error', '/some/file', 275);
Expand All @@ -70,7 +70,7 @@ public function testHandleError() {
*/
public function testHandleFatalError() {
$content = "<error>Fatal Error Error:</error> This is a fatal error in [/some/file, line 275]\n";
ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
$this->stderr->expects($this->once())->method('write')
->with($content);

$this->Error->handleError(E_USER_ERROR, 'This is a fatal error', '/some/file', 275);
Expand All @@ -83,7 +83,7 @@ public function testHandleFatalError() {
*/
public function testCakeErrors() {
$exception = new Error\MissingActionException('Missing action');
ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
$this->stderr->expects($this->once())->method('write')
->with($this->stringContains('Missing action'));

$result = $this->Error->handleException($exception);
Expand All @@ -98,7 +98,7 @@ public function testCakeErrors() {
public function testNonCakeExceptions() {
$exception = new \InvalidArgumentException('Too many parameters.');

ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
$this->stderr->expects($this->once())->method('write')
->with($this->stringContains('Too many parameters.'));

$result = $this->Error->handleException($exception);
Expand All @@ -113,7 +113,7 @@ public function testNonCakeExceptions() {
public function testError404Exception() {
$exception = new Error\NotFoundException('dont use me in cli.');

ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
$this->stderr->expects($this->once())->method('write')
->with($this->stringContains('dont use me in cli.'));

$result = $this->Error->handleException($exception);
Expand All @@ -128,7 +128,7 @@ public function testError404Exception() {
public function testError500Exception() {
$exception = new Error\InternalErrorException('dont use me in cli.');

ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
$this->stderr->expects($this->once())->method('write')
->with($this->stringContains('dont use me in cli.'));

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

0 comments on commit 0889ae5

Please sign in to comment.