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