diff --git a/Async/AsyncAdapter.php b/Async/AsyncAdapter.php index 32e016b..68ff4e5 100644 --- a/Async/AsyncAdapter.php +++ b/Async/AsyncAdapter.php @@ -150,7 +150,8 @@ protected function executeCommand( return null; }); - }, function (\Exception $exception) use ($from, $outputPrinter, $command, $ok, $ko) { + }) + ->otherwise(function (\Throwable $exception) use ($from, $outputPrinter, $command, $ok, $ko) { $to = microtime(true); $ignorable = $exception instanceof MissingHandlerException; @@ -162,16 +163,10 @@ protected function executeCommand( : CommandConsumedLineMessage::REJECTED ))->print($outputPrinter); - return ( - $ignorable - ? (resolve()) - ->then(function () use ($ok) { - return $ok(); - }) - : (resolve()) - ->then(function () use ($ko) { - return $ko(); - }) + return + (resolve($ignorable + ? $ok() + : $ko()) ) ->then(function () { return null; diff --git a/Bus/CommandBus.php b/Bus/CommandBus.php index 6393880..d3a21d0 100644 --- a/Bus/CommandBus.php +++ b/Bus/CommandBus.php @@ -39,9 +39,8 @@ public function execute($command): PromiseInterface return $this ->handle($command) ->then(function () { - return; }); - } catch (\Exception $exception) { + } catch (\Throwable $exception) { return reject($exception); } } diff --git a/Tests/Async/AsyncAdapterTest.php b/Tests/Async/AsyncAdapterTest.php index b6c5c80..a4278d0 100644 --- a/Tests/Async/AsyncAdapterTest.php +++ b/Tests/Async/AsyncAdapterTest.php @@ -24,12 +24,14 @@ use Drift\CommandBus\Tests\Command\ChangeYetAnotherThing; use Drift\CommandBus\Tests\Command\NotRecoverableCommand; use Drift\CommandBus\Tests\Command\RejectException; +use Drift\CommandBus\Tests\Command\ThrowError; use Drift\CommandBus\Tests\Command\ThrowException; use Drift\CommandBus\Tests\CommandHandler\ChangeAnotherThingHandler; use Drift\CommandBus\Tests\CommandHandler\ChangeAThingHandler; use Drift\CommandBus\Tests\CommandHandler\ChangeYetAnotherThingHandler; use Drift\CommandBus\Tests\CommandHandler\NotRecoverableCommandHandler; use Drift\CommandBus\Tests\CommandHandler\RejectExceptionHandler; +use Drift\CommandBus\Tests\CommandHandler\ThrowErrorHandler; use Drift\CommandBus\Tests\CommandHandler\ThrowExceptionHandler; use Drift\CommandBus\Tests\Context; use Drift\CommandBus\Tests\Middleware\Middleware1; @@ -86,6 +88,12 @@ protected static function decorateConfiguration(array $configuration): array ], ]; + $configuration['services'][ThrowErrorHandler::class] = [ + 'tags' => [ + ['name' => 'command_handler', 'method' => 'handle'], + ], + ]; + $configuration['imports'] = [ ['resource' => __DIR__.'/../autowiring.yml'], ]; @@ -285,6 +293,11 @@ public function testReturnRejectedPromise() $process->stop(); } + /** + * @return void + * + * @throws \Drift\CommandBus\Exception\InvalidCommandException + */ public function testNoRecoverableCommand() { $this->resetInfrastructure(); @@ -307,6 +320,33 @@ public function testNoRecoverableCommand() $process->stop(); } + /** + * @return void + * + * @throws \Drift\CommandBus\Exception\InvalidCommandException + */ + public function testErrorIsThrownInHandler() + { + $this->resetInfrastructure(); + + $process = $this->runAsyncCommand([ + 'command-bus:consume-commands', + ]); + + usleep(500000); + + $promises[] = $this + ->getCommandBus() + ->execute(new ThrowError()); + + awaitAll($promises, $this->getLoop()); + usleep(500000); + $output = $process->getOutput(); + $this->assertTrue(1 === substr_count($output, 'Rejected')); + $this->assertStringContainsString("\033[01;31mRejected\033[0m ThrowError", $output); + $process->stop(); + } + /** * Reset infrastructure. * diff --git a/Tests/Async/InMemoryAsyncTest.php b/Tests/Async/InMemoryAsyncTest.php index ce7d391..5b9bad1 100644 --- a/Tests/Async/InMemoryAsyncTest.php +++ b/Tests/Async/InMemoryAsyncTest.php @@ -64,6 +64,11 @@ public function testNoRecoverableCommand() $this->markTestSkipped('InMemory adapter should not fire this test'); } + public function testErrorIsThrownInHandler() + { + $this->markTestSkipped('InMemory adapter should not fire this test'); + } + /** * Consume commands. * diff --git a/Tests/Command/ThrowError.php b/Tests/Command/ThrowError.php new file mode 100644 index 0000000..2c6e22c --- /dev/null +++ b/Tests/Command/ThrowError.php @@ -0,0 +1,22 @@ + + */ + +declare(strict_types=1); + +namespace Drift\CommandBus\Tests\Command; + +use Drift\CommandBus\Bus\NonRecoverableCommand; + +class ThrowError implements NonRecoverableCommand +{ +} diff --git a/Tests/CommandHandler/ThrowErrorHandler.php b/Tests/CommandHandler/ThrowErrorHandler.php new file mode 100644 index 0000000..a99c6b9 --- /dev/null +++ b/Tests/CommandHandler/ThrowErrorHandler.php @@ -0,0 +1,34 @@ + + */ + +declare(strict_types=1); + +namespace Drift\CommandBus\Tests\CommandHandler; + +use Drift\CommandBus\Tests\Command\ThrowError; + +/** + * Class ThrowErrorHandler. + */ +class ThrowErrorHandler +{ + /** + * Handle. + */ + public function handle(ThrowError $throwError) + { + var_dump('N'); + (function (string $a) { + })(1); + } +}