Browse Source

ConsoleErrorHandler uses ConsoleException error code as exit code

Corey Taylor 5 years ago
parent
commit
532147f14a
2 changed files with 20 additions and 19 deletions
  1. 8 1
      src/Error/ConsoleErrorHandler.php
  2. 12 18
      tests/TestCase/Error/ConsoleErrorHandlerTest.php

+ 8 - 1
src/Error/ConsoleErrorHandler.php

@@ -16,7 +16,9 @@ declare(strict_types=1);
  */
 namespace Cake\Error;
 
+use Cake\Command\Command;
 use Cake\Console\ConsoleOutput;
+use Cake\Console\Exception\ConsoleException;
 use Throwable;
 
 /**
@@ -61,7 +63,12 @@ class ConsoleErrorHandler extends BaseErrorHandler
     {
         $this->_displayException($exception);
         $this->logException($exception);
-        $this->_stop(1);
+
+        $exitCode = Command::CODE_ERROR;
+        if ($exception instanceof ConsoleException) {
+            $exitCode = (int)$exception->getCode();
+        }
+        $this->_stop($exitCode);
     }
 
     /**

+ 12 - 18
tests/TestCase/Error/ConsoleErrorHandlerTest.php

@@ -16,9 +16,8 @@ declare(strict_types=1);
  */
 namespace Cake\Test\TestCase\Error;
 
+use Cake\Console\Exception\ConsoleException;
 use Cake\Controller\Exception\MissingActionException;
-use Cake\Http\Exception\InternalErrorException;
-use Cake\Http\Exception\NotFoundException;
 use Cake\Log\Log;
 use Cake\TestSuite\Stub\ConsoleOutput;
 use Cake\TestSuite\TestCase;
@@ -126,33 +125,28 @@ class ConsoleErrorHandlerTest extends TestCase
     {
         $exception = new \InvalidArgumentException('Too many parameters.');
 
+        $this->Error->expects($this->once())
+            ->method('_stop')
+            ->with(1);
+
         $this->Error->handleException($exception);
         $this->assertStringContainsString('Too many parameters', $this->stderr->messages()[0]);
     }
 
     /**
-     * test a Error404 exception.
+     * Test error code is used as exit code for ConsoleException.
      *
      * @return void
      */
-    public function testError404Exception()
+    public function testConsoleExceptions()
     {
-        $exception = new NotFoundException("don't use me in cli.");
-
-        $this->Error->handleException($exception);
-        $this->assertStringContainsString("don't use me in cli", $this->stderr->messages()[0]);
-    }
+        $exception = new ConsoleException('Test ConsoleException', 2);
 
-    /**
-     * test a Error500 exception.
-     *
-     * @return void
-     */
-    public function testError500Exception()
-    {
-        $exception = new InternalErrorException("don't use me in cli.");
+        $this->Error->expects($this->once())
+            ->method('_stop')
+            ->with(2);
 
         $this->Error->handleException($exception);
-        $this->assertStringContainsString("don't use me in cli", $this->stderr->messages()[0]);
+        $this->assertStringContainsString('Test ConsoleException', $this->stderr->messages()[0]);
     }
 }