Skip to content

Commit d59a10a

Browse files
committed
Fix ConsoleErrorHandler not exiting properly.
Implement a _stop() method that allows console exceptions to exit with a non 0 code to indicate failure.
1 parent c3225d3 commit d59a10a

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

lib/Cake/Console/ConsoleErrorHandler.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,13 @@ protected function _displayError($error, $debug) {
9494
$this->_stderr->write($message);
9595
}
9696

97+
/**
98+
* Stop the execution and set the exit code for the process.
99+
*
100+
* @param integer $code The exit code.
101+
*/
102+
protected function _stop($code) {
103+
exit($code);
104+
}
105+
97106
}

lib/Cake/Error/BaseErrorHandler.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ public function handleError($code, $description, $file = null, $line = null, $co
107107
public function handleException($exception) {
108108
$this->_displayException($exception);
109109
$this->_logException($exception);
110+
$this->_stop($exception->getCode() ?: 1);
111+
}
112+
113+
/**
114+
* Stop the process.
115+
*
116+
* Implemented in subclasses that need it.
117+
*
118+
* @param integer $code Exit code.
119+
* @return void
120+
*/
121+
protected function _stop($code) {
122+
// Do nothing.
110123
}
111124

112125
/**

lib/Cake/Error/ExceptionRenderer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Cake\Network\Response;
2626
use Cake\Routing\Router;
2727
use Cake\Utility\Inflector;
28+
use Cake\View\View;
2829

2930
/**
3031
* Exception Renderer.

lib/Cake/Test/TestCase/Console/ConsoleErrorHandlerTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ConsoleErrorHandlerTest extends TestCase {
3333
public function setUp() {
3434
parent::setUp();
3535
$this->stderr = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
36-
$this->Error = new ConsoleErrorHandler(['stderr' => $this->stderr]);
36+
$this->Error = $this->getMock('Cake\Console\ConsoleErrorHandler', ['_stop'], [['stderr' => $this->stderr]]);
3737
}
3838

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

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

89+
$this->Error->expects($this->once())
90+
->method('_stop');
91+
8792
$this->Error->handleException($exception);
8893
}
8994

0 commit comments

Comments
 (0)