Skip to content

Commit b5f9fc9

Browse files
authored
Merge pull request #33 from driftphp/fix/fixed-throwable-in-commands
Fixed when inside a command, a throwable was thrown
2 parents ea02d1c + 460c9e4 commit b5f9fc9

File tree

6 files changed

+108
-13
lines changed

6 files changed

+108
-13
lines changed

Async/AsyncAdapter.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ protected function executeCommand(
150150

151151
return null;
152152
});
153-
}, function (\Exception $exception) use ($from, $outputPrinter, $command, $ok, $ko) {
153+
})
154+
->otherwise(function (\Throwable $exception) use ($from, $outputPrinter, $command, $ok, $ko) {
154155
$to = microtime(true);
155156
$ignorable = $exception instanceof MissingHandlerException;
156157

@@ -162,16 +163,10 @@ protected function executeCommand(
162163
: CommandConsumedLineMessage::REJECTED
163164
))->print($outputPrinter);
164165

165-
return (
166-
$ignorable
167-
? (resolve())
168-
->then(function () use ($ok) {
169-
return $ok();
170-
})
171-
: (resolve())
172-
->then(function () use ($ko) {
173-
return $ko();
174-
})
166+
return
167+
(resolve($ignorable
168+
? $ok()
169+
: $ko())
175170
)
176171
->then(function () {
177172
return null;

Bus/CommandBus.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ public function execute($command): PromiseInterface
3939
return $this
4040
->handle($command)
4141
->then(function () {
42-
return;
4342
});
44-
} catch (\Exception $exception) {
43+
} catch (\Throwable $exception) {
4544
return reject($exception);
4645
}
4746
}

Tests/Async/AsyncAdapterTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
use Drift\CommandBus\Tests\Command\ChangeYetAnotherThing;
2525
use Drift\CommandBus\Tests\Command\NotRecoverableCommand;
2626
use Drift\CommandBus\Tests\Command\RejectException;
27+
use Drift\CommandBus\Tests\Command\ThrowError;
2728
use Drift\CommandBus\Tests\Command\ThrowException;
2829
use Drift\CommandBus\Tests\CommandHandler\ChangeAnotherThingHandler;
2930
use Drift\CommandBus\Tests\CommandHandler\ChangeAThingHandler;
3031
use Drift\CommandBus\Tests\CommandHandler\ChangeYetAnotherThingHandler;
3132
use Drift\CommandBus\Tests\CommandHandler\NotRecoverableCommandHandler;
3233
use Drift\CommandBus\Tests\CommandHandler\RejectExceptionHandler;
34+
use Drift\CommandBus\Tests\CommandHandler\ThrowErrorHandler;
3335
use Drift\CommandBus\Tests\CommandHandler\ThrowExceptionHandler;
3436
use Drift\CommandBus\Tests\Context;
3537
use Drift\CommandBus\Tests\Middleware\Middleware1;
@@ -86,6 +88,12 @@ protected static function decorateConfiguration(array $configuration): array
8688
],
8789
];
8890

91+
$configuration['services'][ThrowErrorHandler::class] = [
92+
'tags' => [
93+
['name' => 'command_handler', 'method' => 'handle'],
94+
],
95+
];
96+
8997
$configuration['imports'] = [
9098
['resource' => __DIR__.'/../autowiring.yml'],
9199
];
@@ -285,6 +293,11 @@ public function testReturnRejectedPromise()
285293
$process->stop();
286294
}
287295

296+
/**
297+
* @return void
298+
*
299+
* @throws \Drift\CommandBus\Exception\InvalidCommandException
300+
*/
288301
public function testNoRecoverableCommand()
289302
{
290303
$this->resetInfrastructure();
@@ -307,6 +320,33 @@ public function testNoRecoverableCommand()
307320
$process->stop();
308321
}
309322

323+
/**
324+
* @return void
325+
*
326+
* @throws \Drift\CommandBus\Exception\InvalidCommandException
327+
*/
328+
public function testErrorIsThrownInHandler()
329+
{
330+
$this->resetInfrastructure();
331+
332+
$process = $this->runAsyncCommand([
333+
'command-bus:consume-commands',
334+
]);
335+
336+
usleep(500000);
337+
338+
$promises[] = $this
339+
->getCommandBus()
340+
->execute(new ThrowError());
341+
342+
awaitAll($promises, $this->getLoop());
343+
usleep(500000);
344+
$output = $process->getOutput();
345+
$this->assertTrue(1 === substr_count($output, 'Rejected'));
346+
$this->assertStringContainsString("\033[01;31mRejected\033[0m ThrowError", $output);
347+
$process->stop();
348+
}
349+
310350
/**
311351
* Reset infrastructure.
312352
*

Tests/Async/InMemoryAsyncTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public function testNoRecoverableCommand()
6464
$this->markTestSkipped('InMemory adapter should not fire this test');
6565
}
6666

67+
public function testErrorIsThrownInHandler()
68+
{
69+
$this->markTestSkipped('InMemory adapter should not fire this test');
70+
}
71+
6772
/**
6873
* Consume commands.
6974
*

Tests/Command/ThrowError.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DriftPHP Project
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <[email protected]>
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Drift\CommandBus\Tests\Command;
17+
18+
use Drift\CommandBus\Bus\NonRecoverableCommand;
19+
20+
class ThrowError implements NonRecoverableCommand
21+
{
22+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DriftPHP Project
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <[email protected]>
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Drift\CommandBus\Tests\CommandHandler;
17+
18+
use Drift\CommandBus\Tests\Command\ThrowError;
19+
20+
/**
21+
* Class ThrowErrorHandler.
22+
*/
23+
class ThrowErrorHandler
24+
{
25+
/**
26+
* Handle.
27+
*/
28+
public function handle(ThrowError $throwError)
29+
{
30+
var_dump('N');
31+
(function (string $a) {
32+
})(1);
33+
}
34+
}

0 commit comments

Comments
 (0)