Skip to content

Commit

Permalink
custom exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick in t Veld committed May 16, 2024
1 parent 9e02d6f commit 3804fd3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Builder/Uri/UriBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Builder\Uri;

use App\Exception\ClassNotFoundException;

class UriBuilder
{
/**
Expand All @@ -19,7 +21,7 @@ public function build(string $builder, array $parameters): string
'login' => (new LoginUriBuilder())($parameters),
'logout' => (new LogoutUriBuilder())($parameters),
'widget' => (new WidgetUriBuilder())($parameters),
default => throw new \Exception('Builder not found!'),
default => throw new ClassNotFoundException($builder),
};
}
}
10 changes: 7 additions & 3 deletions src/Dto/Aggregator/AggregateRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace App\Dto\Aggregator;

use App\Exception\AccountNotFoundException;
use App\Exception\NoDataFoundException;
use App\Exception\SessionNotFoundException;

class AggregateRoot implements AggregateInterface
{
/** @var array<mixed> */
Expand All @@ -22,7 +26,7 @@ public function setAccounts(array $accounts): void
public function getAccounts(): array
{
if (empty($this->accounts)) {
throw new \Exception('No accounts found!');
throw new AccountNotFoundException();
}

return $this->accounts;
Expand All @@ -36,7 +40,7 @@ public function setData(mixed $data): void
public function getData(): mixed
{
if ($this->data === null) {
throw new \Exception('No data found!');
throw new NoDataFoundException();
}

return $this->data;
Expand All @@ -50,7 +54,7 @@ public function setSession(string $session): void
public function getSession(): string
{
if (null === $this->session) {
throw new \Exception('The session is missing');
throw new SessionNotFoundException();
}

return $this->session;
Expand Down
13 changes: 13 additions & 0 deletions src/Exception/ClassNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Exception;

class ClassNotFoundException extends \RuntimeException
{
public function __construct(string $class)
{
parent::__construct(sprintf('Class %s not found!', $class));
}
}
13 changes: 13 additions & 0 deletions src/Exception/NoDataFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Exception;

class NoDataFoundException extends \RuntimeException
{
public function __construct()
{
parent::__construct('No data found!');
}
}
3 changes: 2 additions & 1 deletion src/Manager/ActionHandlerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Manager;

use App\ActionHandler\ActionHandlerInterface;
use App\Exception\ClassNotFoundException;

class ActionHandlerManager
{
Expand All @@ -21,7 +22,7 @@ public function __invoke(string $handler): ActionHandlerInterface
'daily_data' => $this->dailyDataActionHandler,
'daily_gain' => $this->dailyGainActionHandler,
'history' => $this->historyActionHandler,
default => throw new \Exception('Handler not found!'),
default => throw new ClassNotFoundException($handler),
};
}
}

0 comments on commit 3804fd3

Please sign in to comment.