1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Silviooosilva\CacheerPhp\Exceptions; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use JsonSerializable; |
9
|
|
|
|
10
|
|
|
class BaseException extends Exception implements JsonSerializable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Details about the exception. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
**/ |
17
|
|
|
protected array $details; |
18
|
|
|
|
19
|
|
|
public function __construct(string $message = "", int $code = 0, ?Exception $previous = null, array $details = []) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($message, $code, $previous); |
22
|
|
|
$this->details = $details; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get the details of the exception. |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
**/ |
30
|
|
|
public function getDetails() |
31
|
|
|
{ |
32
|
|
|
return $this->details; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set the details of the exception. |
37
|
|
|
* |
38
|
|
|
* @param array $details |
39
|
|
|
* */ |
40
|
|
|
public function setDetails(array $details) |
41
|
|
|
{ |
42
|
|
|
$this->details = $details; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Convert the exception to an array representation. |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
* */ |
50
|
|
|
public function toArray() |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
'type' => static::class, |
54
|
|
|
'message' => $this->getMessage(), |
55
|
|
|
'code' => $this->getCode(), |
56
|
|
|
'details' => $this->getDetails(), |
57
|
|
|
'file' => $this->getFile(), |
58
|
|
|
'line' => $this->getLine(), |
59
|
|
|
'trace' => $this->getTrace() |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Convert the exception to a JSON serializable format. |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
* */ |
68
|
|
|
public function jsonSerialize(): array |
69
|
|
|
{ |
70
|
|
|
return $this->toArray(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Convert the exception to a JSON string. |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
* */ |
78
|
|
|
public function toJson(int $options = 0) |
79
|
|
|
{ |
80
|
|
|
return json_encode($this->toArray(), $options | JSON_THROW_ON_ERROR); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Convert the exception to a string representation. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
* */ |
88
|
|
|
public function __toString() |
89
|
|
|
{ |
90
|
|
|
return sprintf( |
91
|
|
|
"[%s] %s in %s on line %d\nDetails: %s", |
92
|
|
|
$this->getCode(), |
93
|
|
|
$this->getMessage(), |
94
|
|
|
$this->getFile(), |
95
|
|
|
$this->getLine(), |
96
|
|
|
json_encode($this->getDetails(), JSON_PRETTY_PRINT) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|