Skip to content

Commit

Permalink
Ensure ConsoleErrorHandler exits with code 1 for exceptions with stri…
Browse files Browse the repository at this point in the history
…ng codes.

Code was messed up during 2.6 => 3.0 merge.
  • Loading branch information
ADmad committed Jul 3, 2014
1 parent 4275717 commit b31fb19
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
17 changes: 17 additions & 0 deletions src/Console/ConsoleErrorHandler.php
Expand Up @@ -50,6 +50,23 @@ public function __construct($options = []) {
$this->_options = $options;
}

/**
* Handle errors in the console environment. Writes errors to stderr,
* and logs messages if Configure::read('debug') is false.
*
* @param \Exception $exception Exception instance.
* @return void
* @throws Exception When renderer class not found
* @see http://php.net/manual/en/function.set-exception-handler.php
*/
public function handleException(\Exception $exception) {
$this->_displayException($exception);
$this->_logException($exception);
$code = $exception->getCode();
$code = ($code && is_int($code)) ? $code : 1;
$this->_stop($code);
}

/**
* Prints an exception to stderr.
*
Expand Down
10 changes: 3 additions & 7 deletions tests/TestCase/Console/ConsoleErrorHandlerTest.php
Expand Up @@ -141,18 +141,14 @@ public function testError500Exception() {
* @return void
*/
public function testNonIntegerExceptionCode() {
if (PHP_VERSION_ID < 50300) {
$this->markTestSkipped('ReflectionProperty::setAccessible() is available since 5.3');
}
$exception = new Error\Exception('Non-integer exception code');

$exception = new Exception('Non-integer exception code');

$class = new ReflectionClass('Exception');
$class = new \ReflectionClass('Exception');
$property = $class->getProperty('code');
$property->setAccessible(true);
$property->setValue($exception, '42S22');

ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
$this->stderr->expects($this->once())->method('write')
->with($this->stringContains('Non-integer exception code'));

$this->Error->expects($this->once())
Expand Down

0 comments on commit b31fb19

Please sign in to comment.