-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExceptionItem.php
executable file
·144 lines (125 loc) · 3.37 KB
/
ExceptionItem.php
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* @Package: MaplePHP - Exception item
* @Author: Daniel Ronkainen
* @Licence: Apache-2.0 license, Copyright © Daniel Ronkainen
Don't delete this comment, its part of the license.
*/
namespace MaplePHP\Blunder;
use Throwable;
class ExceptionItem
{
private int $flag;
private Throwable $exception;
private SeverityLevelPool $pool;
public function __construct(Throwable $exception, ?SeverityLevelPool $pool = null)
{
$this->exception = $exception;
$this->flag = (method_exists($exception, "getSeverity")) ? $exception->getSeverity() : 0;
if(is_null($pool)) {
$pool = new SeverityLevelPool();
}
$this->pool = $pool;
}
/**
* Will return error message
* @return string
*/
public function __toString(): string
{
return $this->exception->getMessage();
}
/**
* Access the exception
* @param string $name
* @param array $args
* @return mixed
*/
public function __call(string $name, array $args): mixed
{
if(!method_exists($this->exception, $name)) {
throw new \BadMethodCallException("Method '$name' does not exist in Throwable class");
}
return $this->exception->{$name}(...$args);
}
/**
* Get Exception
* @return Throwable
*/
public function getException(): Throwable
{
return $this->exception;
}
/**
* Get exception type
* @return string
*/
public function getType(): string
{
return get_class($this->exception);
}
/**
* Delete a severity level
* @return bool
*/
public function deleteSeverity(): bool
{
return $this->pool->deleteSeverityLevel($this->flag);
}
/**
* Check if error type is supported
* @return bool
*/
public function hasSeverity(): bool
{
return ($this->pool->has($this->flag) !== false);
}
/**
* Get severity level title name if severity is callable else will return exception type
* @return string|null
*/
public function getSeverity(): ?string
{
if($this->flag === 0) {
return $this->getType();
}
return SeverityLevelPool::getSeverityLevel($this->flag);
}
/**
* Get severity level title name
* @return int
*/
public function getMask(): int
{
return $this->flag;
}
/**
* This will return a status for severity flag that will follow and return a
* status title that follows PSR-3 log for easily logging errors
* @return string
*/
public function getStatus(): string
{
return match ($this->flag) {
E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, 0 => "error",
E_WARNING, E_USER_WARNING, E_COMPILE_WARNING => "warning",
E_NOTICE, E_USER_NOTICE => "notice",
E_STRICT, E_DEPRECATED, E_USER_DEPRECATED => "info",
default => "debug",
};
}
/**
* Check if error is an fatal error
* @return bool
*/
final public function isLevelFatal(): bool
{
$errors = E_ERROR;
$errors |= E_PARSE;
$errors |= E_CORE_ERROR;
$errors |= E_CORE_WARNING;
$errors |= E_COMPILE_ERROR;
$errors |= E_COMPILE_WARNING;
return ($this->flag & $errors) > 0;
}
}