Skip to content

Commit f4a883a

Browse files
committed
feat(exceptions): add new exception classes for various modules
- Created `AbstractException` as the base exception class - Added `AuthenticationException` and `AuthorizationException` under `Auth` - Added `CacheException` under `Cache` - Added `ConfigurationException` under `Config` - Added `ContainerException` under `Container` - Added `DatabaseException` under `Database` - Added `EventException` under `Event` - Added `ExternalServiceException` under `ExternalService` - Added `FileException` under `File` - Added `InputException` under `Input` - Added `LocalizationException` under `Localization` - Added `MiddlewareException` under `Middleware` - Added `NetworkException` and `HttpException` under `Network` - Added `QueueException` under `Queue` - Added `RoutingException` under `Routing` - Added `RuntimeException` under `Runtime` - Added `SecurityException` and `EncryptionException` under `Security` - Added `SessionException` under `Session` - Added `SystemException` under `System` - Added `TemplateException` under `Template` - Added `ValidationException` and `RuleViolationException` under `Validation`
1 parent b95e3be commit f4a883a

30 files changed

+569
-6
lines changed

src/AbstractErrorMessage.php

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/AbstractException.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
11
<?php
22

33
declare(strict_types=1);
4+
5+
namespace KaririCode\Exception;
6+
7+
use KaririCode\Exception\Contract\ErrorMessage;
8+
use KaririCode\Exception\Contract\Throwable;
9+
10+
abstract class AbstractException extends \Exception implements Throwable
11+
{
12+
protected array $context = [];
13+
14+
public function __construct(ErrorMessage $errorMessage, ?\Throwable $previous = null, array $context = [])
15+
{
16+
parent::__construct($errorMessage->getMessage(), 0, $previous);
17+
$this->context = $context;
18+
}
19+
20+
public function getContext(): array
21+
{
22+
return $this->context;
23+
}
24+
25+
protected function addContext(string $key, mixed $value): self
26+
{
27+
$this->context[$key] = $value;
28+
29+
return $this;
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
11
<?php
22

33
declare(strict_types=1);
4+
5+
namespace KaririCode\Exception\Auth;
6+
7+
use KaririCode\Exception\AbstractException;
8+
use KaririCode\Exception\ExceptionMessage;
9+
10+
final class AuthenticationException extends AbstractException
11+
{
12+
public static function invalidCredentials(): self
13+
{
14+
return new self(new ExceptionMessage('INVALID_CREDENTIALS', 'Invalid credentials provided'));
15+
}
16+
17+
public static function accountLocked(): self
18+
{
19+
return new self(new ExceptionMessage('ACCOUNT_LOCKED', 'Account is locked'));
20+
}
21+
22+
public static function twoFactorRequired(): self
23+
{
24+
return new self(new ExceptionMessage('TWO_FACTOR_REQUIRED', 'Two-factor authentication is required'));
25+
}
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
11
<?php
22

33
declare(strict_types=1);
4+
5+
namespace KaririCode\Exception\Auth;
6+
7+
use KaririCode\Exception\AbstractException;
8+
use KaririCode\Exception\ExceptionMessage;
9+
10+
final class AuthorizationException extends AbstractException
11+
{
12+
public static function insufficientPermissions(string $action): self
13+
{
14+
return new self(new ExceptionMessage('INSUFFICIENT_PERMISSIONS', "Insufficient permissions for action: {$action}"));
15+
}
16+
17+
public static function roleRequired(string $role): self
18+
{
19+
return new self(new ExceptionMessage('ROLE_REQUIRED', "Required role not present: {$role}"));
20+
}
21+
}

src/Cache/CacheException.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
11
<?php
22

33
declare(strict_types=1);
4+
5+
namespace KaririCode\Exception\Cache;
6+
7+
use KaririCode\Exception\AbstractException;
8+
use KaririCode\Exception\ExceptionMessage;
9+
10+
final class CacheException extends AbstractException
11+
{
12+
public static function itemNotFound(string $key): self
13+
{
14+
return new self(new ExceptionMessage('CACHE_ITEM_NOT_FOUND', "Cache item not found: {$key}"));
15+
}
16+
17+
public static function storageError(string $details): self
18+
{
19+
return new self(new ExceptionMessage('CACHE_STORAGE_ERROR', "Cache storage error: {$details}"));
20+
}
21+
22+
public static function invalidTtl(int $ttl): self
23+
{
24+
return new self(new ExceptionMessage('INVALID_TTL', "Invalid TTL value: {$ttl}"));
25+
}
26+
}

src/CommonErrorMessages.php

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
11
<?php
22

33
declare(strict_types=1);
4+
5+
namespace KaririCode\Exception\Config;
6+
7+
use KaririCode\Exception\AbstractException;
8+
use KaririCode\Exception\ExceptionMessage;
9+
10+
final class ConfigurationException extends AbstractException
11+
{
12+
public static function missingKey(string $key): self
13+
{
14+
return new self(new ExceptionMessage('MISSING_CONFIG_KEY', "Missing configuration key: {$key}"));
15+
}
16+
17+
public static function invalidValue(string $key, mixed $value): self
18+
{
19+
return new self(new ExceptionMessage('INVALID_CONFIG_VALUE', "Invalid configuration value for key '{$key}': " . var_export($value, true)));
20+
}
21+
22+
public static function environmentNotSet(string $envVar): self
23+
{
24+
return new self(new ExceptionMessage('ENVIRONMENT_NOT_SET', "Environment variable not set: {$envVar}"));
25+
}
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
11
<?php
22

33
declare(strict_types=1);
4+
5+
namespace KaririCode\Exception\Container;
6+
7+
use KaririCode\Exception\AbstractException;
8+
use KaririCode\Exception\ExceptionMessage;
9+
10+
final class ContainerException extends AbstractException
11+
{
12+
public static function serviceNotFound(string $serviceId): self
13+
{
14+
return new self(new ExceptionMessage('SERVICE_NOT_FOUND', "Service not found in container: {$serviceId}"));
15+
}
16+
17+
public static function circularDependency(string $serviceId): self
18+
{
19+
return new self(new ExceptionMessage('CIRCULAR_DEPENDENCY', "Circular dependency detected for service: {$serviceId}"));
20+
}
21+
}

src/Contract/ErrorMessage.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
<?php
22

33
declare(strict_types=1);
4+
5+
namespace KaririCode\Exception\Contract;
6+
7+
interface ErrorMessage
8+
{
9+
public function getCode(): string;
10+
11+
public function getMessage(): string;
12+
}

src/Contract/Throwable.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
<?php
22

33
declare(strict_types=1);
4+
5+
namespace KaririCode\Exception\Contract;
6+
7+
use Throwable as GlobalThrowable;
8+
9+
interface Throwable extends GlobalThrowable
10+
{
11+
public function getContext(): array;
12+
}

0 commit comments

Comments
 (0)