Skip to content

Commit

Permalink
Fix ConsoleErrorHandler not exiting properly.
Browse files Browse the repository at this point in the history
Implement a _stop() method that allows console exceptions to exit with
a non 0 code to indicate failure.
  • Loading branch information
markstory committed Aug 22, 2013
1 parent c3225d3 commit d59a10a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/Cake/Console/ConsoleErrorHandler.php
Expand Up @@ -94,4 +94,13 @@ protected function _displayError($error, $debug) {
$this->_stderr->write($message);
}

/**
* Stop the execution and set the exit code for the process.
*
* @param integer $code The exit code.
*/
protected function _stop($code) {
exit($code);
}

}
13 changes: 13 additions & 0 deletions lib/Cake/Error/BaseErrorHandler.php
Expand Up @@ -107,6 +107,19 @@ public function handleError($code, $description, $file = null, $line = null, $co
public function handleException($exception) {
$this->_displayException($exception);
$this->_logException($exception);
$this->_stop($exception->getCode() ?: 1);
}

/**
* Stop the process.
*
* Implemented in subclasses that need it.
*
* @param integer $code Exit code.
* @return void
*/
protected function _stop($code) {
// Do nothing.
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/Cake/Error/ExceptionRenderer.php
Expand Up @@ -25,6 +25,7 @@
use Cake\Network\Response;
use Cake\Routing\Router;
use Cake\Utility\Inflector;
use Cake\View\View;

/**
* Exception Renderer.
Expand Down
7 changes: 6 additions & 1 deletion lib/Cake/Test/TestCase/Console/ConsoleErrorHandlerTest.php
Expand Up @@ -33,7 +33,7 @@ class ConsoleErrorHandlerTest extends TestCase {
public function setUp() {
parent::setUp();
$this->stderr = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
$this->Error = new ConsoleErrorHandler(['stderr' => $this->stderr]);
$this->Error = $this->getMock('Cake\Console\ConsoleErrorHandler', ['_stop'], [['stderr' => $this->stderr]]);
}

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

$this->Error->handleError(E_NOTICE, 'This is a notice error', '/some/file', 275);
}
Expand Down Expand Up @@ -84,6 +86,9 @@ public function testCakeErrors() {
$this->stderr->expects($this->once())->method('write')
->with($this->stringContains($message));

$this->Error->expects($this->once())
->method('_stop');

$this->Error->handleException($exception);
}

Expand Down

0 comments on commit d59a10a

Please sign in to comment.