From 4d49833666bd41eff8980fb0f9eb937cc01dc40f Mon Sep 17 00:00:00 2001 From: Rick in t Veld Date: Thu, 25 Apr 2024 16:11:40 +0200 Subject: [PATCH] Created exceptions for the actions --- src/Action/Account/FetchAccounts.php | 3 ++- src/Action/Account/FetchTradingAccounts.php | 3 ++- src/Action/Account/FetchUserSession.php | 3 ++- src/Action/Download/FileDownloadAction.php | 3 ++- src/Error/InstanceError.php | 13 +++++++++++++ src/Exception/AccountNotFoundException.php | 13 +++++++++++++ src/Exception/SessionNotFoundException.php | 13 +++++++++++++ 7 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/Error/InstanceError.php create mode 100644 src/Exception/AccountNotFoundException.php create mode 100644 src/Exception/SessionNotFoundException.php diff --git a/src/Action/Account/FetchAccounts.php b/src/Action/Account/FetchAccounts.php index a140dd2..565bc5e 100644 --- a/src/Action/Account/FetchAccounts.php +++ b/src/Action/Account/FetchAccounts.php @@ -9,6 +9,7 @@ use App\Dto\Aggregator\AggregateInterface; use App\Event\CreateAccountEvent; use App\Event\UpdateAccountEvent; +use App\Exception\AccountNotFoundException; use App\Repository\AccountRepository; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -30,7 +31,7 @@ public function __invoke(AggregateInterface $aggregator): void } if (empty($myFxBookAccounts)) { - throw new \Exception('No accounts found!'); + throw new AccountNotFoundException(); } $accounts = $this->accountRepository->findAll(); diff --git a/src/Action/Account/FetchTradingAccounts.php b/src/Action/Account/FetchTradingAccounts.php index caa2da7..fa1601a 100644 --- a/src/Action/Account/FetchTradingAccounts.php +++ b/src/Action/Account/FetchTradingAccounts.php @@ -8,6 +8,7 @@ use App\Contract\Repository\MyFxBookRepositoryInterface; use App\Dto\Aggregator\AggregateInterface; use App\Event\CreateAccountEvent; +use App\Exception\AccountNotFoundException; use Symfony\Component\EventDispatcher\EventDispatcherInterface; final readonly class FetchTradingAccounts implements ActionInterface @@ -23,7 +24,7 @@ public function __invoke(AggregateInterface $aggregator): void $accounts = $this->myFxBookRepository->accounts($aggregator->getSession()); if (count($accounts) === 0) { - throw new \Exception('No accounts found'); + throw new AccountNotFoundException(); } foreach ($accounts as $account) { diff --git a/src/Action/Account/FetchUserSession.php b/src/Action/Account/FetchUserSession.php index 96ff90c..a0c3c87 100644 --- a/src/Action/Account/FetchUserSession.php +++ b/src/Action/Account/FetchUserSession.php @@ -6,6 +6,7 @@ use App\Action\ActionInterface; use App\Dto\Aggregator\AggregateInterface; +use App\Exception\SessionNotFoundException; use App\Repository\UserRepository; final readonly class FetchUserSession implements ActionInterface @@ -19,7 +20,7 @@ public function __invoke(AggregateInterface $aggregator): void $user = $this->userRepository->findLatest(); if (null === $user || null === $user->getSession()) { - throw new \Exception('Could not find a user session'); + throw new SessionNotFoundException(); } $aggregator->setSession($user->getSession()); diff --git a/src/Action/Download/FileDownloadAction.php b/src/Action/Download/FileDownloadAction.php index c452134..df9cd49 100644 --- a/src/Action/Download/FileDownloadAction.php +++ b/src/Action/Download/FileDownloadAction.php @@ -6,6 +6,7 @@ use App\Action\ActionInterface; use App\Dto\Aggregator\AggregateInterface; +use App\Error\InstanceError; use App\FileSystem\File; use App\Manager\FileDownloadManager; @@ -18,7 +19,7 @@ public function __construct(private FileDownloadManager $fileDownloadManager) public function __invoke(AggregateInterface $aggregator): void { if (($aggregator->getData() instanceof File) === false) { - throw new \RuntimeException('Data should be an instance of class ' . File::class); + throw new InstanceError(File::class); } ($this->fileDownloadManager)($aggregator->getData()); diff --git a/src/Error/InstanceError.php b/src/Error/InstanceError.php new file mode 100644 index 0000000..7648e7a --- /dev/null +++ b/src/Error/InstanceError.php @@ -0,0 +1,13 @@ +