Skip to content

Commit 6514f7d

Browse files
committed
refactor(exceptions): centralize exception creation using AbstractException::createException
- Update all exception classes to use the createException method from AbstractException - This change reduces code duplication and improves consistency across exception handling - Affected files include all custom exception classes in the src/ directory
1 parent fd75dbd commit 6514f7d

25 files changed

+121
-141
lines changed

src/AbstractException.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public function __construct(ErrorMessage $errorMessage, ?\Throwable $previous =
1616
{
1717
$this->errorCode = $errorMessage->getErrorCode();
1818
$this->context = array_merge(['code' => $this->errorCode], $context);
19-
2019
parent::__construct($errorMessage->getMessage(), $errorMessage->getCode(), $previous);
2120
}
2221

@@ -36,4 +35,9 @@ protected function addContext(string $key, mixed $value): self
3635

3736
return $this;
3837
}
38+
39+
protected static function createException(int $code, string $errorCode, string $message): self
40+
{
41+
return new static(new ExceptionMessage($code, $errorCode, $message));
42+
}
3943
}

src/Auth/AuthenticationException.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace KaririCode\Exception\Auth;
66

77
use KaririCode\Exception\AbstractException;
8-
use KaririCode\Exception\ExceptionMessage;
98

109
final class AuthenticationException extends AbstractException
1110
{
@@ -15,28 +14,28 @@ final class AuthenticationException extends AbstractException
1514

1615
public static function invalidCredentials(): self
1716
{
18-
return new self(new ExceptionMessage(
17+
return self::createException(
1918
self::CODE_INVALID_CREDENTIALS,
2019
'INVALID_CREDENTIALS',
2120
'Invalid credentials provided'
22-
));
21+
);
2322
}
2423

2524
public static function accountLocked(): self
2625
{
27-
return new self(new ExceptionMessage(
26+
return self::createException(
2827
self::CODE_ACCOUNT_LOCKED,
2928
'ACCOUNT_LOCKED',
3029
'Account is locked'
31-
));
30+
);
3231
}
3332

3433
public static function twoFactorRequired(): self
3534
{
36-
return new self(new ExceptionMessage(
35+
return self::createException(
3736
self::CODE_TWO_FACTOR_REQUIRED,
3837
'TWO_FACTOR_REQUIRED',
3938
'Two-factor authentication is required'
40-
));
39+
);
4140
}
4241
}

src/Auth/AuthorizationException.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace KaririCode\Exception\Auth;
66

77
use KaririCode\Exception\AbstractException;
8-
use KaririCode\Exception\ExceptionMessage;
98

109
final class AuthorizationException extends AbstractException
1110
{
@@ -14,19 +13,19 @@ final class AuthorizationException extends AbstractException
1413

1514
public static function insufficientPermissions(string $action): self
1615
{
17-
return new self(new ExceptionMessage(
16+
return self::createException(
1817
self::CODE_INSUFFICIENT_PERMISSIONS,
1918
'INSUFFICIENT_PERMISSIONS',
2019
"Insufficient permissions for action: {$action}"
21-
));
20+
);
2221
}
2322

2423
public static function roleRequired(string $role): self
2524
{
26-
return new self(new ExceptionMessage(
25+
return self::createException(
2726
self::CODE_ROLE_REQUIRED,
2827
'ROLE_REQUIRED',
2928
"Required role not present: {$role}"
30-
));
29+
);
3130
}
3231
}

src/Cache/CacheException.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace KaririCode\Exception\Cache;
66

77
use KaririCode\Exception\AbstractException;
8-
use KaririCode\Exception\ExceptionMessage;
98

109
final class CacheException extends AbstractException
1110
{
@@ -15,28 +14,28 @@ final class CacheException extends AbstractException
1514

1615
public static function itemNotFound(string $key): self
1716
{
18-
return new self(new ExceptionMessage(
17+
return self::createException(
1918
self::CODE_ITEM_NOT_FOUND,
2019
'CACHE_ITEM_NOT_FOUND',
2120
"Cache item not found: {$key}"
22-
));
21+
);
2322
}
2423

2524
public static function storageError(string $details): self
2625
{
27-
return new self(new ExceptionMessage(
26+
return self::createException(
2827
self::CODE_STORAGE_ERROR,
2928
'CACHE_STORAGE_ERROR',
3029
"Cache storage error: {$details}"
31-
));
30+
);
3231
}
3332

3433
public static function invalidTtl(int $ttl): self
3534
{
36-
return new self(new ExceptionMessage(
35+
return self::createException(
3736
self::CODE_INVALID_TTL,
3837
'INVALID_TTL',
3938
"Invalid TTL value: {$ttl}"
40-
));
39+
);
4140
}
4241
}

src/Config/ConfigurationException.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace KaririCode\Exception\Config;
66

77
use KaririCode\Exception\AbstractException;
8-
use KaririCode\Exception\ExceptionMessage;
98

109
final class ConfigurationException extends AbstractException
1110
{
@@ -15,28 +14,28 @@ final class ConfigurationException extends AbstractException
1514

1615
public static function missingKey(string $key): self
1716
{
18-
return new self(new ExceptionMessage(
17+
return self::createException(
1918
self::CODE_MISSING_KEY,
2019
'MISSING_CONFIG_KEY',
2120
"Missing configuration key: {$key}"
22-
));
21+
);
2322
}
2423

2524
public static function invalidValue(string $key, mixed $value): self
2625
{
27-
return new self(new ExceptionMessage(
26+
return self::createException(
2827
self::CODE_INVALID_VALUE,
2928
'INVALID_CONFIG_VALUE',
3029
"Invalid configuration value for key '{$key}': " . var_export($value, true)
31-
));
30+
);
3231
}
3332

3433
public static function environmentNotSet(string $envVar): self
3534
{
36-
return new self(new ExceptionMessage(
35+
return self::createException(
3736
self::CODE_ENVIRONMENT_NOT_SET,
3837
'ENVIRONMENT_NOT_SET',
3938
"Environment variable not set: {$envVar}"
40-
));
39+
);
4140
}
4241
}

src/Container/ContainerException.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace KaririCode\Exception\Container;
66

77
use KaririCode\Exception\AbstractException;
8-
use KaririCode\Exception\ExceptionMessage;
98

109
final class ContainerException extends AbstractException
1110
{
@@ -14,19 +13,19 @@ final class ContainerException extends AbstractException
1413

1514
public static function serviceNotFound(string $serviceId): self
1615
{
17-
return new self(new ExceptionMessage(
16+
return self::createException(
1817
self::CODE_SERVICE_NOT_FOUND,
1918
'SERVICE_NOT_FOUND',
2019
"Service not found in container: {$serviceId}"
21-
));
20+
);
2221
}
2322

2423
public static function circularDependency(string $serviceId): self
2524
{
26-
return new self(new ExceptionMessage(
25+
return self::createException(
2726
self::CODE_CIRCULAR_DEPENDENCY,
2827
'CIRCULAR_DEPENDENCY',
2928
"Circular dependency detected for service: {$serviceId}"
30-
));
29+
);
3130
}
3231
}

src/Database/DatabaseException.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace KaririCode\Exception\Database;
66

77
use KaririCode\Exception\AbstractException;
8-
use KaririCode\Exception\ExceptionMessage;
98

109
final class DatabaseException extends AbstractException
1110
{
@@ -15,28 +14,28 @@ final class DatabaseException extends AbstractException
1514

1615
public static function connectionError(string $details): self
1716
{
18-
return new self(new ExceptionMessage(
17+
return self::createException(
1918
self::CODE_CONNECTION_ERROR,
2019
'DB_CONNECTION_ERROR',
2120
"Database connection error: {$details}"
22-
));
21+
);
2322
}
2423

2524
public static function queryError(string $query, string $error): self
2625
{
27-
return new self(new ExceptionMessage(
26+
return self::createException(
2827
self::CODE_QUERY_ERROR,
2928
'DB_QUERY_ERROR',
3029
"Database query error: {$error}"
31-
));
30+
);
3231
}
3332

3433
public static function deadlockDetected(): self
3534
{
36-
return new self(new ExceptionMessage(
35+
return self::createException(
3736
self::CODE_DEADLOCK_DETECTED,
3837
'DB_DEADLOCK',
3938
'Database deadlock detected'
40-
));
39+
);
4140
}
4241
}

src/Event/EventException.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace KaririCode\Exception\Event;
66

77
use KaririCode\Exception\AbstractException;
8-
use KaririCode\Exception\ExceptionMessage;
98

109
final class EventException extends AbstractException
1110
{
@@ -14,19 +13,19 @@ final class EventException extends AbstractException
1413

1514
public static function listenerNotCallable(string $eventName): self
1615
{
17-
return new self(new ExceptionMessage(
16+
return self::createException(
1817
self::CODE_LISTENER_NOT_CALLABLE,
1918
'LISTENER_NOT_CALLABLE',
2019
"Event listener is not callable for event: {$eventName}"
21-
));
20+
);
2221
}
2322

2423
public static function eventDispatchFailed(string $eventName): self
2524
{
26-
return new self(new ExceptionMessage(
25+
return self::createException(
2726
self::CODE_EVENT_DISPATCH_FAILED,
2827
'EVENT_DISPATCH_FAILED',
2928
"Failed to dispatch event: {$eventName}"
30-
));
29+
);
3130
}
3231
}

src/ExternalService/ExternalServiceException.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace KaririCode\Exception\ExternalService;
66

77
use KaririCode\Exception\AbstractException;
8-
use KaririCode\Exception\ExceptionMessage;
98

109
final class ExternalServiceException extends AbstractException
1110
{
@@ -15,28 +14,28 @@ final class ExternalServiceException extends AbstractException
1514

1615
public static function apiError(string $service, string $error): self
1716
{
18-
return new self(new ExceptionMessage(
17+
return self::createException(
1918
self::CODE_API_ERROR,
2019
'API_ERROR',
2120
"Error from external service '{$service}': {$error}"
22-
));
21+
);
2322
}
2423

2524
public static function serviceUnavailable(string $service): self
2625
{
27-
return new self(new ExceptionMessage(
26+
return self::createException(
2827
self::CODE_SERVICE_UNAVAILABLE,
2928
'SERVICE_UNAVAILABLE',
3029
"External service unavailable: {$service}"
31-
));
30+
);
3231
}
3332

3433
public static function invalidResponse(string $service): self
3534
{
36-
return new self(new ExceptionMessage(
35+
return self::createException(
3736
self::CODE_INVALID_RESPONSE,
3837
'INVALID_RESPONSE',
3938
"Invalid response from external service: {$service}"
40-
));
39+
);
4140
}
4241
}

src/File/FileException.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace KaririCode\Exception\File;
66

77
use KaririCode\Exception\AbstractException;
8-
use KaririCode\Exception\ExceptionMessage;
98

109
final class FileException extends AbstractException
1110
{
@@ -16,37 +15,37 @@ final class FileException extends AbstractException
1615

1716
public static function notFound(string $path): self
1817
{
19-
return new self(new ExceptionMessage(
18+
return self::createException(
2019
self::CODE_NOT_FOUND,
2120
'FILE_NOT_FOUND',
2221
"File not found: {$path}"
23-
));
22+
);
2423
}
2524

2625
public static function permissionDenied(string $path): self
2726
{
28-
return new self(new ExceptionMessage(
27+
return self::createException(
2928
self::CODE_PERMISSION_DENIED,
3029
'PERMISSION_DENIED',
3130
"Permission denied for file: {$path}"
32-
));
31+
);
3332
}
3433

3534
public static function unreadable(string $path): self
3635
{
37-
return new self(new ExceptionMessage(
36+
return self::createException(
3837
self::CODE_UNREADABLE,
3938
'FILE_UNREADABLE',
4039
"File is not readable: {$path}"
41-
));
40+
);
4241
}
4342

4443
public static function uploadFailed(string $filename): self
4544
{
46-
return new self(new ExceptionMessage(
45+
return self::createException(
4746
self::CODE_UPLOAD_FAILED,
4847
'UPLOAD_FAILED',
4948
"Failed to upload file: {$filename}"
50-
));
49+
);
5150
}
5251
}

0 commit comments

Comments
 (0)