diff --git a/README.md b/README.md index 56ce2c5..b0e44a6 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,21 @@ $speed1 === $speed2; // is true > See detailed implementation proposal: [jeckel-lab/query-dispatcher](https://github.com/Jeckel-Lab/query-dispatcher) +## Exceptions + +Each layer has it's own Exception interface that extends `Throwable`: + +- Core: [CoreException](src/Core/Exception/CoreException.php) +- Domain: [DomainException](src/Domain/Exception/DomainException.php) +- Infrastructure: [InfrastructureException](src/Infrastructure/Exception/InfrastructureException.php) +- Presentation: [PresentationException](src/Presentation/Exception/PresentationException.php) + +In each layer, when we need to throw an Exception, we create a new class corresponding to the type of Exception. This class must: + +- extends one of the [SPL exception](https://www.php.net/manual/en/spl.exceptions.php) or another (more generic) exception from the same namespace. +- implements the exception interface of the current layer. + + ## Old Documentation ### Core diff --git a/src/Core/CommandDispatcher/CommandBus/CommandBus.php b/src/Core/CommandDispatcher/CommandBus/CommandBus.php index 9c80716..2d015f1 100644 --- a/src/Core/CommandDispatcher/CommandBus/CommandBus.php +++ b/src/Core/CommandDispatcher/CommandBus/CommandBus.php @@ -9,6 +9,7 @@ use JeckelLab\Contract\Core\CommandDispatcher\Command\Command; use JeckelLab\Contract\Core\CommandDispatcher\CommandResponse\CommandResponse; +use JeckelLab\Contract\Core\CommandDispatcher\Exception\NoHandlerDefinedForCommandException; /** * Interface CommandBus @@ -19,6 +20,7 @@ interface CommandBus /** * @param Command $command * @return CommandResponse + * @throws NoHandlerDefinedForCommandException */ public function dispatch(Command $command): CommandResponse; } diff --git a/src/Core/CommandDispatcher/CommandBus/Exception/CommandBusException.php b/src/Core/CommandDispatcher/CommandBus/Exception/CommandBusException.php deleted file mode 100644 index 9ec882f..0000000 --- a/src/Core/CommandDispatcher/CommandBus/Exception/CommandBusException.php +++ /dev/null @@ -1,19 +0,0 @@ - - * Created at: 03/12/2021 - */ - -namespace JeckelLab\Contract\Core\CommandDispatcher\CommandBus\Exception; - -use JeckelLab\Contract\Core\CommandDispatcher\Exception\CommandDispatcherException; - -/** - * Interface CommandBusException - * @package JeckelLab\Contract\Core\CommandDispatcher\CommandBus\Exception - * @psalm-immutable - */ -interface CommandBusException extends CommandDispatcherException -{ -} diff --git a/src/Core/CommandDispatcher/CommandHandler/CommandHandler.php b/src/Core/CommandDispatcher/CommandHandler/CommandHandler.php index d4c9f84..1da7c12 100644 --- a/src/Core/CommandDispatcher/CommandHandler/CommandHandler.php +++ b/src/Core/CommandDispatcher/CommandHandler/CommandHandler.php @@ -9,7 +9,7 @@ use JeckelLab\Contract\Core\CommandDispatcher\Command\Command; use JeckelLab\Contract\Core\CommandDispatcher\CommandResponse\CommandResponse; -use JeckelLab\Contract\Core\CommandDispatcher\Exception\InvalidCommandException; +use JeckelLab\Contract\Core\CommandDispatcher\Exception\InvalidCommandTypeException; /** * Interface CommandHandler @@ -27,7 +27,7 @@ public static function getHandledCommands(): array; /** * @param CommandType $command * @return CommandResponse - * @throws InvalidCommandException + * @throws InvalidCommandTypeException */ public function __invoke(Command $command): CommandResponse; } diff --git a/src/Core/CommandDispatcher/Exception/CommandDispatcherException.php b/src/Core/CommandDispatcher/Exception/CommandDispatcherException.php index 03f5a9f..c535cfd 100644 --- a/src/Core/CommandDispatcher/Exception/CommandDispatcherException.php +++ b/src/Core/CommandDispatcher/Exception/CommandDispatcherException.php @@ -7,13 +7,13 @@ namespace JeckelLab\Contract\Core\CommandDispatcher\Exception; -use JeckelLab\Contract\Core\Exception\CoreMainException; +use JeckelLab\Contract\Core\Exception\CoreException; /** * Interface CommandDispatcherException * @package JeckelLab\Contract\Core\CommandDispatcher\Exception * @psalm-immutable */ -interface CommandDispatcherException extends CoreMainException +interface CommandDispatcherException extends CoreException { } diff --git a/src/Core/CommandDispatcher/Exception/InvalidCommandException.php b/src/Core/CommandDispatcher/Exception/InvalidCommandTypeException.php similarity index 67% rename from src/Core/CommandDispatcher/Exception/InvalidCommandException.php rename to src/Core/CommandDispatcher/Exception/InvalidCommandTypeException.php index 5bedf28..8716c93 100644 --- a/src/Core/CommandDispatcher/Exception/InvalidCommandException.php +++ b/src/Core/CommandDispatcher/Exception/InvalidCommandTypeException.php @@ -9,13 +9,14 @@ namespace JeckelLab\Contract\Core\CommandDispatcher\Exception; -use JeckelLab\Contract\Core\Exception\RuntimeException; +use LogicException; /** * Class InvalidCommandException * @package JeckelLab\Contract\Core\CommandDispatcher\Exception * @psalm-immutable + * @psalm-suppress MutableDependency */ -class InvalidCommandException extends RuntimeException implements CommandDispatcherException +class InvalidCommandTypeException extends LogicException implements CommandDispatcherException { } diff --git a/src/Core/CommandDispatcher/CommandBus/Exception/NoHandlerDefinedForCommandException.php b/src/Core/CommandDispatcher/Exception/NoHandlerDefinedForCommandException.php similarity index 69% rename from src/Core/CommandDispatcher/CommandBus/Exception/NoHandlerDefinedForCommandException.php rename to src/Core/CommandDispatcher/Exception/NoHandlerDefinedForCommandException.php index e8149f7..60734b0 100644 --- a/src/Core/CommandDispatcher/CommandBus/Exception/NoHandlerDefinedForCommandException.php +++ b/src/Core/CommandDispatcher/Exception/NoHandlerDefinedForCommandException.php @@ -7,17 +7,18 @@ declare(strict_types=1); -namespace JeckelLab\Contract\Core\CommandDispatcher\CommandBus\Exception; +namespace JeckelLab\Contract\Core\CommandDispatcher\Exception; use JeckelLab\Contract\Core\CommandDispatcher\Command\Command; -use JeckelLab\Contract\Core\Exception\LogicException; +use LogicException; /** * Class NoHandlerDefinedForCommandException - * @package JeckelLab\Contract\Core\CommandDispatcher\CommandBus\Exception + * @package JeckelLab\Contract\Core\CommandDispatcher\Exception * @psalm-immutable + * @psalm-suppress MutableDependency */ -class NoHandlerDefinedForCommandException extends LogicException implements CommandBusException +class NoHandlerDefinedForCommandException extends LogicException implements CommandDispatcherException { /** * @param Command $command diff --git a/src/Core/Exception/CoreMainException.php b/src/Core/Exception/CoreException.php similarity index 72% rename from src/Core/Exception/CoreMainException.php rename to src/Core/Exception/CoreException.php index 1a4df11..9feef21 100644 --- a/src/Core/Exception/CoreMainException.php +++ b/src/Core/Exception/CoreException.php @@ -7,13 +7,13 @@ namespace JeckelLab\Contract\Core\Exception; -use JeckelLab\Contract\Exception\MainException; +use Throwable; /** * Interface CoreMainException * @package JeckelLab\Contract\Core\Exception * @psalm-immutable */ -interface CoreMainException extends MainException +interface CoreException extends Throwable { } diff --git a/src/Core/Exception/LogicException.php b/src/Core/Exception/LogicException.php deleted file mode 100644 index f16ada7..0000000 --- a/src/Core/Exception/LogicException.php +++ /dev/null @@ -1,20 +0,0 @@ - - * Created at: 03/12/2021 - */ - -declare(strict_types=1); - -namespace JeckelLab\Contract\Core\Exception; - -/** - * Class LogicException - * @package JeckelLab\Contract\Core\Exception - * @psalm-immutable - * @psalm-suppress MutableDependency - */ -class LogicException extends \LogicException implements CoreMainException -{ -} diff --git a/src/Core/Exception/RuntimeException.php b/src/Core/Exception/RuntimeException.php deleted file mode 100644 index 4f1a3d4..0000000 --- a/src/Core/Exception/RuntimeException.php +++ /dev/null @@ -1,20 +0,0 @@ - - * Created at: 03/12/2021 - */ - -declare(strict_types=1); - -namespace JeckelLab\Contract\Core\Exception; - -/** - * Class RuntimeException - * @package JeckelLab\Contract\Core\Exception - * @psalm-immutable - * @psalm-suppress MutableDependency - */ -class RuntimeException extends \RuntimeException implements CoreMainException -{ -} diff --git a/src/Core/QueryDispatcher/Exception/InvalidQueryException.php b/src/Core/QueryDispatcher/Exception/InvalidQueryTypeException.php similarity index 70% rename from src/Core/QueryDispatcher/Exception/InvalidQueryException.php rename to src/Core/QueryDispatcher/Exception/InvalidQueryTypeException.php index c4dff0b..bc31bfd 100644 --- a/src/Core/QueryDispatcher/Exception/InvalidQueryException.php +++ b/src/Core/QueryDispatcher/Exception/InvalidQueryTypeException.php @@ -9,7 +9,7 @@ namespace JeckelLab\Contract\Core\QueryDispatcher\Exception; -use JeckelLab\Contract\Core\Exception\RuntimeException; +use LogicException; /** * Class InvalidQueryException @@ -17,6 +17,6 @@ * @psalm-immutable * @psalm-suppress MutableDependency */ -class InvalidQueryException extends RuntimeException implements QueryDispatcherException +class InvalidQueryTypeException extends LogicException implements QueryDispatcherException { } diff --git a/src/Core/QueryDispatcher/QueryBus/Exception/NoHandlerDefinedForQueryException.php b/src/Core/QueryDispatcher/Exception/NoHandlerDefinedForQueryException.php similarity index 71% rename from src/Core/QueryDispatcher/QueryBus/Exception/NoHandlerDefinedForQueryException.php rename to src/Core/QueryDispatcher/Exception/NoHandlerDefinedForQueryException.php index 4958f18..5011498 100644 --- a/src/Core/QueryDispatcher/QueryBus/Exception/NoHandlerDefinedForQueryException.php +++ b/src/Core/QueryDispatcher/Exception/NoHandlerDefinedForQueryException.php @@ -7,18 +7,18 @@ declare(strict_types=1); -namespace JeckelLab\Contract\Core\QueryDispatcher\QueryBus\Exception; +namespace JeckelLab\Contract\Core\QueryDispatcher\Exception; -use JeckelLab\Contract\Core\Exception\LogicException; use JeckelLab\Contract\Core\QueryDispatcher\Query\Query; +use LogicException; /** * Class NoHandlerDefinedForQueryException - * @package JeckelLab\Contract\Core\QueryDispatcher\QueryBus\Exception + * @package JeckelLab\Contract\Core\QueryDispatcher\Exception * @psalm-immutable * @psalm-suppress MutableDependency */ -class NoHandlerDefinedForQueryException extends LogicException implements QueryBusException +class NoHandlerDefinedForQueryException extends LogicException implements QueryDispatcherException { /** * @param Query $query diff --git a/src/Core/QueryDispatcher/Exception/QueryDispatcherException.php b/src/Core/QueryDispatcher/Exception/QueryDispatcherException.php index 31515a3..aa0f2a6 100644 --- a/src/Core/QueryDispatcher/Exception/QueryDispatcherException.php +++ b/src/Core/QueryDispatcher/Exception/QueryDispatcherException.php @@ -7,13 +7,13 @@ namespace JeckelLab\Contract\Core\QueryDispatcher\Exception; -use JeckelLab\Contract\Core\Exception\CoreMainException; +use JeckelLab\Contract\Core\Exception\CoreException; /** * Interface QueryDispatcherException * @package JeckelLab\Contract\Core\QueryDispatcher\Exception * @psalm-immutable */ -interface QueryDispatcherException extends CoreMainException +interface QueryDispatcherException extends CoreException { } diff --git a/src/Core/QueryDispatcher/QueryBus/Exception/QueryBusException.php b/src/Core/QueryDispatcher/QueryBus/Exception/QueryBusException.php deleted file mode 100644 index 0129630..0000000 --- a/src/Core/QueryDispatcher/QueryBus/Exception/QueryBusException.php +++ /dev/null @@ -1,19 +0,0 @@ - - * Created at: 08/12/2021 - */ - -namespace JeckelLab\Contract\Core\QueryDispatcher\QueryBus\Exception; - -use JeckelLab\Contract\Core\QueryDispatcher\Exception\QueryDispatcherException; - -/** - * Class QueryBusException - * @package JeckelLab\Contract\Core\QueryDispatcher\QueryBus\Exception - * @psalm-immutable - */ -interface QueryBusException extends QueryDispatcherException -{ -} diff --git a/src/Core/QueryDispatcher/QueryBus/QueryBus.php b/src/Core/QueryDispatcher/QueryBus/QueryBus.php index 127f0e1..86a0663 100644 --- a/src/Core/QueryDispatcher/QueryBus/QueryBus.php +++ b/src/Core/QueryDispatcher/QueryBus/QueryBus.php @@ -7,6 +7,7 @@ namespace JeckelLab\Contract\Core\QueryDispatcher\QueryBus; +use JeckelLab\Contract\Core\QueryDispatcher\Exception\NoHandlerDefinedForQueryException; use JeckelLab\Contract\Core\QueryDispatcher\Query\Query; use JeckelLab\Contract\Core\QueryDispatcher\ViewModel\ViewModel; @@ -15,6 +16,7 @@ interface QueryBus /** * @param Query $query * @return ViewModel + * @throws NoHandlerDefinedForQueryException */ public function dispatch(Query $query): ViewModel; } diff --git a/src/Core/QueryDispatcher/QueryHandler/QueryHandler.php b/src/Core/QueryDispatcher/QueryHandler/QueryHandler.php index 824e73f..acab472 100644 --- a/src/Core/QueryDispatcher/QueryHandler/QueryHandler.php +++ b/src/Core/QueryDispatcher/QueryHandler/QueryHandler.php @@ -7,7 +7,7 @@ namespace JeckelLab\Contract\Core\QueryDispatcher\QueryHandler; -use JeckelLab\Contract\Core\QueryDispatcher\Exception\InvalidQueryException; +use JeckelLab\Contract\Core\QueryDispatcher\Exception\InvalidQueryTypeException; use JeckelLab\Contract\Core\QueryDispatcher\Query\Query; use JeckelLab\Contract\Core\QueryDispatcher\ViewModel\ViewModel; @@ -22,7 +22,7 @@ public static function getHandledQueries(): array; /** * @param Query $query * @return ViewModel - * @throws InvalidQueryException + * @throws InvalidQueryTypeException */ public function __invoke(Query $query): ViewModel; } diff --git a/src/Domain/Exception/InvalidArgumentException.php b/src/Domain/Exception/InvalidArgumentException.php deleted file mode 100644 index a492be6..0000000 --- a/src/Domain/Exception/InvalidArgumentException.php +++ /dev/null @@ -1,20 +0,0 @@ - - * Created at: 15/06/2020 - */ - -declare(strict_types=1); - -namespace JeckelLab\Contract\Domain\Exception; - -/** - * Class InvalidArgumentException - * @package JeckelLab\Contract\Domain\Exception - * @psalm-immutable - * @psalm-suppress MutableDependency - */ -class InvalidArgumentException extends \InvalidArgumentException implements DomainException -{ -} diff --git a/src/Domain/Exception/LogicException.php b/src/Domain/Exception/LogicException.php deleted file mode 100644 index 0287de9..0000000 --- a/src/Domain/Exception/LogicException.php +++ /dev/null @@ -1,20 +0,0 @@ - - * Created at: 15/06/2020 - */ - -declare(strict_types=1); - -namespace JeckelLab\Contract\Domain\Exception; - -/** - * Class LogicException - * @package JeckelLab\Contract\Domain\Exception - * @psalm-immutable - * @psalm-suppress MutableDependency - */ -class LogicException extends \LogicException implements DomainException -{ -} diff --git a/src/Domain/Exception/RuntimeException.php b/src/Domain/Exception/RuntimeException.php deleted file mode 100644 index f384144..0000000 --- a/src/Domain/Exception/RuntimeException.php +++ /dev/null @@ -1,20 +0,0 @@ - - * Created at: 15/06/2020 - */ - -declare(strict_types=1); - -namespace JeckelLab\Contract\Domain\Exception; - -/** - * Class RuntimeException - * @package JeckelLab\Contract\Domain\Exception - * @psalm-immutable - * @psalm-suppress MutableDependency - */ -class RuntimeException extends \RuntimeException implements DomainException -{ -} diff --git a/src/Domain/Identity/Exception/InvalidIdException.php b/src/Domain/Identity/Exception/InvalidIdException.php index dc66882..f3420a4 100644 --- a/src/Domain/Identity/Exception/InvalidIdException.php +++ b/src/Domain/Identity/Exception/InvalidIdException.php @@ -9,7 +9,7 @@ namespace JeckelLab\Contract\Domain\Identity\Exception; -use JeckelLab\Contract\Domain\Exception\InvalidArgumentException; +use InvalidArgumentException; /** * Class InvalidIdException @@ -18,6 +18,7 @@ * * @package JeckelLab\Contract\Domain\Identity\Exception * @psalm-immutable + * @psalm-suppress MutableDependency */ class InvalidIdException extends InvalidArgumentException implements IdentityException { diff --git a/src/Domain/ValueObject/Exception/InvalidArgumentException.php b/src/Domain/ValueObject/Exception/InvalidArgumentException.php index f3904ce..52cfc3b 100644 --- a/src/Domain/ValueObject/Exception/InvalidArgumentException.php +++ b/src/Domain/ValueObject/Exception/InvalidArgumentException.php @@ -9,13 +9,14 @@ namespace JeckelLab\Contract\Domain\ValueObject\Exception; -use JeckelLab\Contract\Domain\Exception\InvalidArgumentException as DomainInvalidArgumentException; +use InvalidArgumentException as InvalidArgumentExceptionCore; /** * Class InvalidArgumentException * @package ValueObject\Exception * @psalm-immutable + * @psalm-suppress MutableDependency */ -class InvalidArgumentException extends DomainInvalidArgumentException implements ValueObjectException +class InvalidArgumentException extends InvalidArgumentExceptionCore implements ValueObjectException { } diff --git a/src/Exception/MainException.php b/src/Exception/MainException.php deleted file mode 100644 index fd56d0d..0000000 --- a/src/Exception/MainException.php +++ /dev/null @@ -1,21 +0,0 @@ - - * Created at: 18/11/2021 - */ - -declare(strict_types=1); - -namespace JeckelLab\Contract\Exception; - -use Throwable; - -/** - * Interface ExceptionInterface - * @package JeckelLab\Contract\Exception - * @psalm-immutable - */ -interface MainException extends Throwable -{ -} diff --git a/src/Infrastructure/Exception/InfrastructureMainException.php b/src/Infrastructure/Exception/InfrastructureException.php similarity index 72% rename from src/Infrastructure/Exception/InfrastructureMainException.php rename to src/Infrastructure/Exception/InfrastructureException.php index 12a738b..e43e08f 100644 --- a/src/Infrastructure/Exception/InfrastructureMainException.php +++ b/src/Infrastructure/Exception/InfrastructureException.php @@ -7,13 +7,13 @@ namespace JeckelLab\Contract\Infrastructure\Exception; -use JeckelLab\Contract\Exception\MainException; +use Throwable; /** * Interface InfrastructureException * @package JeckelLab\Contract\Infrastructure\Exception * @psalm-immutable */ -interface InfrastructureMainException extends MainException +interface InfrastructureException extends Throwable { } diff --git a/src/Presentation/Exception/PresentationMainException.php b/src/Presentation/Exception/PresentationException.php similarity index 72% rename from src/Presentation/Exception/PresentationMainException.php rename to src/Presentation/Exception/PresentationException.php index d89eee5..d1db8c7 100644 --- a/src/Presentation/Exception/PresentationMainException.php +++ b/src/Presentation/Exception/PresentationException.php @@ -7,13 +7,13 @@ namespace JeckelLab\Contract\Presentation\Exception; -use JeckelLab\Contract\Exception\MainException; +use Throwable; /** * Interface PresentationException * @package JeckelLab\Contract\Presentation\Exception * @psalm-immutable */ -interface PresentationMainException extends MainException +interface PresentationException extends Throwable { } diff --git a/tests/Core/CommandDispatcher/CommandBus/Exception/NoHandlerDefinedForCommandExceptionTest.php b/tests/Core/CommandDispatcher/CommandBus/Exception/NoHandlerDefinedForCommandExceptionTest.php index 0cefbf0..a7680f7 100644 --- a/tests/Core/CommandDispatcher/CommandBus/Exception/NoHandlerDefinedForCommandExceptionTest.php +++ b/tests/Core/CommandDispatcher/CommandBus/Exception/NoHandlerDefinedForCommandExceptionTest.php @@ -7,7 +7,7 @@ namespace Tests\JeckelLab\Contract\Core\CommandDispatcher\CommandBus\Exception; -use JeckelLab\Contract\Core\CommandDispatcher\CommandBus\Exception\NoHandlerDefinedForCommandException; +use JeckelLab\Contract\Core\CommandDispatcher\Exception\NoHandlerDefinedForCommandException; use PHPUnit\Framework\TestCase; /** diff --git a/tests/Core/QueryDispatcher/QueryBus/Exception/NoHandlerDefinedForQueryExceptionTest.php b/tests/Core/QueryDispatcher/QueryBus/Exception/NoHandlerDefinedForQueryExceptionTest.php index 11753c7..98bd592 100644 --- a/tests/Core/QueryDispatcher/QueryBus/Exception/NoHandlerDefinedForQueryExceptionTest.php +++ b/tests/Core/QueryDispatcher/QueryBus/Exception/NoHandlerDefinedForQueryExceptionTest.php @@ -7,7 +7,7 @@ namespace Tests\JeckelLab\Contract\Core\QueryDispatcher\QueryBus\Exception; -use JeckelLab\Contract\Core\QueryDispatcher\QueryBus\Exception\NoHandlerDefinedForQueryException; +use JeckelLab\Contract\Core\QueryDispatcher\Exception\NoHandlerDefinedForQueryException; use PHPUnit\Framework\TestCase; /**