1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Koded package. |
5
|
|
|
* |
6
|
|
|
* (c) Mihail Binev <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Please view the LICENSE distributed with this source code |
9
|
|
|
* for the full copyright and license information. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Koded\Exceptions; |
13
|
|
|
|
14
|
|
|
use Koded\Stdlib\{Data, Serializer}; |
15
|
|
|
use RuntimeException; |
16
|
|
|
use Throwable; |
17
|
|
|
use function strtr; |
18
|
|
|
|
19
|
|
|
class KodedException extends RuntimeException |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array A map of exception code => message entries |
23
|
|
|
*/ |
24
|
|
|
protected array $messages = [ |
25
|
|
|
Data::E_PHP_EXCEPTION => '[Exception] :message', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* KodedException constructor. |
30
|
|
|
* |
31
|
|
|
* @param int $code As defined in the child classes |
32
|
|
|
* @param array $arguments [optional] If ['message' => ''] exists in $arguments, |
33
|
|
|
* this will be the error message, meaning the messages |
34
|
|
|
* defined by $code in the child classes are ignored |
35
|
|
|
* @param Throwable|null $previous [optional] The previous throwable used for the exception chaining |
36
|
|
|
*/ |
37
|
9 |
|
public function __construct(int $code, array $arguments = [], Throwable $previous = null) |
38
|
|
|
{ |
39
|
9 |
|
$message = $arguments['message'] ?? $this->messages[$code] ?? $this->message; |
40
|
9 |
|
parent::__construct(strtr($message, $arguments), $code, $previous); |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
public static function generic(string $message, Throwable $previous = null): static |
44
|
|
|
{ |
45
|
1 |
|
return new static(Data::E_PHP_EXCEPTION, [':message' => $message], $previous); |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
public static function from(Throwable $exception): static |
49
|
|
|
{ |
50
|
2 |
|
return new static($exception->getCode(), ['message' => $exception->getMessage()], $exception); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
class ReadOnlyException extends KodedException |
56
|
|
|
{ |
57
|
|
|
protected array $messages = [ |
58
|
|
|
Data::E_CLONING_DISALLOWED => 'Cloning the :class instance is not allowed', |
59
|
|
|
Data::E_READONLY_INSTANCE => 'Cannot set :index. :class instance is read-only', |
60
|
|
|
]; |
61
|
|
|
|
62
|
2 |
|
public static function forInstance(string $index, string $class): static |
63
|
|
|
{ |
64
|
2 |
|
return new static(Data::E_READONLY_INSTANCE, [':index' => $index, ':class' => $class]); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public static function forCloning(string $class): static |
68
|
|
|
{ |
69
|
2 |
|
return new static(Data::E_CLONING_DISALLOWED, [':class' => $class]); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
class SerializerException extends KodedException |
75
|
|
|
{ |
76
|
|
|
protected array $messages = [ |
77
|
|
|
Serializer::E_INVALID_SERIALIZER => 'Failed to create a serializer for ":name"', |
78
|
|
|
Serializer::E_MISSING_MODULE => '[Dependency error] ":module" module is not installed on this machine', |
79
|
|
|
]; |
80
|
|
|
|
81
|
1 |
|
public static function forMissingModule(string $module): static |
82
|
|
|
{ |
83
|
1 |
|
return new static(Serializer::E_MISSING_MODULE, [':module' => $module]); |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
public static function forCreateSerializer(string $name): static |
87
|
|
|
{ |
88
|
2 |
|
return new static(Serializer::E_INVALID_SERIALIZER, [':name' => $name]); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|