Skip to content

Commit

Permalink
Prefer command handlers to not throw exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stratadox committed Sep 21, 2019
1 parent bb3608c commit 5e498e0
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
build/
.idea/
.phpunit.result.cache
3 changes: 0 additions & 3 deletions contracts/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

namespace Stratadox\CommandHandling;

use Throwable;

interface Handler
{
/**
* Handles an inputted command, either performing an action or throwing an
* exception.
*
* @param object $command The command to handle.
* @throws Throwable When the command is denied.
*/
public function handle(object $command): void;
}
1 change: 0 additions & 1 deletion src/OnExceptionCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public function handle(object $command): void
$this->handler->handle($command);
} catch (Throwable $exception) {
$this->middleware->invoke($command, $exception);
throw $exception;
}
}
}
9 changes: 2 additions & 7 deletions tests/InvokingMiddlewareInExceptionCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@ function invoking_the_middleware_for_rejected_commands()
AlwaysFailingCommand::class => new AlwaysThrowingHandler(),
]));

try {
$input->handle(new AlwaysFailingCommand());
$input->handle(new AlwaysFailingCommand());

$this->fail('Expecting an exception while handling the command.');
} catch (Throwable $exception) {
$this->assertInstanceOf(CommandDenied::class, $exception);
}
$this->assertInstanceOf(
AlwaysFailingCommand::class,
$middleware->observedCommand()
Expand Down Expand Up @@ -77,7 +72,7 @@ function invoking_the_middleware_for_specific_commands_only()
try {
$input->handle(new AlwaysFailingCommand());

$this->fail('Expecting an exception while handling the command.');
$this->fail('Exceptions should not be caught by default.');
} catch (Throwable $exception) {
$this->assertInstanceOf(CommandDenied::class, $exception);
}
Expand Down

0 comments on commit 5e498e0

Please sign in to comment.