-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
PhpError.php
195 lines (176 loc) · 4.19 KB
/
PhpError.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 4.4.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Error;
/**
* Object wrapper around PHP errors that are emitted by `trigger_error()`
*/
class PhpError
{
/**
* @var int
*/
private int $code;
/**
* @var string
*/
private string $message;
/**
* @var string|null
*/
private ?string $file;
/**
* @var int|null
*/
private ?int $line;
/**
* Stack trace data. Each item should have a `reference`, `file` and `line` keys.
*
* @var array<array<string, int>>
*/
private array $trace;
/**
* @var array<int, string>
*/
private array $levelMap = [
E_PARSE => 'error',
E_ERROR => 'error',
E_CORE_ERROR => 'error',
E_COMPILE_ERROR => 'error',
E_USER_ERROR => 'error',
E_WARNING => 'warning',
E_USER_WARNING => 'warning',
E_COMPILE_WARNING => 'warning',
E_RECOVERABLE_ERROR => 'warning',
E_NOTICE => 'notice',
E_USER_NOTICE => 'notice',
E_STRICT => 'strict',
E_DEPRECATED => 'deprecated',
E_USER_DEPRECATED => 'deprecated',
];
/**
* @var array<string, int>
*/
private array $logMap = [
'error' => LOG_ERR,
'warning' => LOG_WARNING,
'notice' => LOG_NOTICE,
'strict' => LOG_NOTICE,
'deprecated' => LOG_NOTICE,
];
/**
* Constructor
*
* @param int $code The PHP error code constant
* @param string $message The error message.
* @param string|null $file The filename of the error.
* @param int|null $line The line number for the error.
* @param array $trace The backtrace for the error.
*/
public function __construct(
int $code,
string $message,
?string $file = null,
?int $line = null,
array $trace = []
) {
$this->code = $code;
$this->message = $message;
$this->file = $file;
$this->line = $line;
$this->trace = $trace;
}
/**
* Get the PHP error constant.
*
* @return int
*/
public function getCode(): int
{
return $this->code;
}
/**
* Get the mapped LOG_ constant.
*
* @return int
*/
public function getLogLevel(): int
{
$label = $this->getLabel();
return $this->logMap[$label] ?? LOG_ERR;
}
/**
* Get the error code label
*
* @return string
*/
public function getLabel(): string
{
return $this->levelMap[$this->code] ?? 'error';
}
/**
* Get the error message.
*
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
/**
* Get the error file
*
* @return string|null
*/
public function getFile(): ?string
{
return $this->file;
}
/**
* Get the error line number.
*
* @return int|null
*/
public function getLine(): ?int
{
return $this->line;
}
/**
* Get the stacktrace as an array.
*
* @return array
*/
public function getTrace(): array
{
return $this->trace;
}
/**
* Get the stacktrace as a string.
*
* @return string
*/
public function getTraceAsString(): string
{
$out = [];
foreach ($this->trace as $frame) {
if (!empty($frame['line'])) {
$out[] = "{$frame['reference']} {$frame['file']}, line {$frame['line']}";
} else {
$out[] = $frame['reference'];
}
}
return implode("\n", $out);
}
}