From fda590741afdff2580eed90284739f29afdb5ca6 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 21 Aug 2013 15:56:30 -0400 Subject: [PATCH] Start moving ConsoleErrorHandler to use new base class. Update code and tests for ConsoleErrorHandler to work with new template method and base class. --- lib/Cake/Console/ConsoleErrorHandler.php | 74 +++++++++---------- lib/Cake/Error/BaseErrorHandler.php | 31 ++++++++ .../Console/ConsoleErrorHandlerTest.php | 8 +- 3 files changed, 69 insertions(+), 44 deletions(-) diff --git a/lib/Cake/Console/ConsoleErrorHandler.php b/lib/Cake/Console/ConsoleErrorHandler.php index 82eea5e0523..90beffdfc57 100644 --- a/lib/Cake/Console/ConsoleErrorHandler.php +++ b/lib/Cake/Console/ConsoleErrorHandler.php @@ -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. @@ -50,21 +52,6 @@ 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. * @@ -72,40 +59,45 @@ public function register() { * @return integer Exit code from exception caught. */ public function handleException(\Exception $exception) { - $this->_stderr->write(__d('cake_console', "Error: %s\n%s", + $errorName = __d('cake_console', 'Error:'); + if ($exception instanceof FatalErrorException) { + $errorName = __d('cake_console', 'Fatal Error:'); + } + $message = sprintf( + "%s %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', "%s 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', "%s Error: %s\n", + $error['error'], + $message + ); + $this->_stderr->write($message); } } diff --git a/lib/Cake/Error/BaseErrorHandler.php b/lib/Cake/Error/BaseErrorHandler.php index 4a9406e6031..aad8295aa69 100644 --- a/lib/Cake/Error/BaseErrorHandler.php +++ b/lib/Cake/Error/BaseErrorHandler.php @@ -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. * diff --git a/lib/Cake/Test/TestCase/Console/ConsoleErrorHandlerTest.php b/lib/Cake/Test/TestCase/Console/ConsoleErrorHandlerTest.php index 1d43893a862..a44c2c61bf1 100644 --- a/lib/Cake/Test/TestCase/Console/ConsoleErrorHandlerTest.php +++ b/lib/Cake/Test/TestCase/Console/ConsoleErrorHandlerTest.php @@ -65,9 +65,10 @@ public function testHandleError() { * @return void */ public function testHandleFatalError() { - $content = "Fatal Error Error: This is a fatal error in [/some/file, line 275]\n"; + ob_start(); + $content = "Fatal 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); } @@ -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);