Repository navigation
Expand file tree
/
Copy pathExceptionSnapshot.php
More file actions
286 lines (257 loc) · 8.51 KB
/
Copy pathExceptionSnapshot.php
File metadata and controls
286 lines (257 loc) · 8.51 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
declare(strict_types=1);
namespace PHPForge\Debug\Storage;
use JsonSerializable;
use PHPForge\Debug\Capture\CapturePolicy;
use PHPForge\Debug\Helper\{Coerce, Fqcn};
use SensitiveParameter;
use Stringable;
use Throwable;
use function array_map;
use function array_replace;
use function is_int;
use function is_string;
/**
* Represents a captured throwable without executable state.
*/
final readonly class ExceptionSnapshot implements JsonSerializable, Stringable
{
/**
* Frame arguments stay in their tagged form so a snapshot read back from disk re-serializes byte for byte; newly
* captured snapshots deliberately persist an empty argument list.
*
* @param string $class Captured throwable class.
* @param string $message Captured throwable message.
* @param int|string $code Captured throwable code.
* @param string $file File where the throwable originated.
* @param int $line Line where the throwable originated.
* @param list<array{
* namespace: string,
* short_class: string,
* class: string,
* type: string,
* function: string|null,
* file: string|null,
* line: int|null,
* args: DebugArray,
* }> $trace Captured trace frames.
* @param string $toString Original throwable text.
* @param self|null $previous Previous throwable snapshot or `null`.
*/
public function __construct(
private string $class,
private string $message,
private int|string $code,
private string $file,
private int $line,
private array $trace,
private string $toString,
private self|null $previous,
) {}
/**
* Returns the original throwable text captured in the snapshot.
*
* @return string Original throwable text.
*/
public function __toString(): string
{
return $this->toString;
}
/**
* Hydrates a throwable snapshot from decoded JSON data.
*
* @param mixed $data Decoded throwable payload.
* @param string $path Payload path used in hydration errors.
*
* @return self Hydrated throwable snapshot.
*/
public static function fromArray(mixed $data, string $path = '$.exception'): self
{
$payload = Payload::object($data, $path)
->shape(
[
'class',
'message',
'code',
'file',
'line',
'trace',
'toString',
'previous',
],
);
$rawCode = $payload->raw('code');
if (!is_int($rawCode) && !is_string($rawCode)) {
throw HydrationException::at(
"{$path}.code",
'an integer or string',
);
}
$trace = [];
foreach ($payload->list('trace') as $index => $rawFrame) {
$framePath = "{$path}.trace[{$index}]";
$frame = Payload::object($rawFrame, $framePath)
->shape(
[
'namespace',
'short_class',
'class',
'type',
'function',
'file',
'line',
'args',
],
);
$trace[] = [
'namespace' => $frame->string('namespace'),
'short_class' => $frame->string('short_class'),
'class' => $frame->string('class'),
'type' => $frame->string('type'),
'function' => $frame->nullableString('function'),
'file' => $frame->nullableString('file'),
'line' => $frame->nullableInt('line'),
'args' => DebugArray::fromArray($frame->raw('args'), "{$framePath}.args"),
];
}
$previous = $payload->raw('previous');
return new self(
class: $payload->string('class'),
message: $payload->string('message'),
code: $rawCode,
file: $payload->string('file'),
line: $payload->int('line'),
trace: $trace,
toString: $payload->string('toString'),
previous: $previous === null ? null : self::fromArray($previous, "{$path}.previous"),
);
}
/**
* Captures a throwable and its previous-exception chain without executable state.
*
* @param Throwable $throwable Throwable to capture.
*
* @return self Captured throwable snapshot.
*/
public static function fromThrowable(#[SensitiveParameter] Throwable $throwable): self
{
$capturePolicy = new CapturePolicy();
$trace = [];
foreach ($throwable->getTrace() as $entry) {
$class = Coerce::string($entry['class'] ?? null);
$trace[] = [
'namespace' => Json::safeString(Fqcn::namespacePart($class)),
'short_class' => Json::safeString(Fqcn::shortName($class)),
'class' => Json::safeString($class),
'type' => Json::safeString(Coerce::string($entry['type'] ?? null)),
'function' => Json::safeString($entry['function']),
'file' => is_string($entry['file'] ?? null) ? Json::safeString($entry['file']) : null,
'line' => is_int($entry['line'] ?? null) ? $entry['line'] : null,
'args' => DebugArray::capture([]),
];
}
$code = $throwable->getCode();
try {
$toString = (string) $throwable;
} catch (Throwable) {
$toString = $throwable::class . ': ' . $throwable->getMessage();
}
return new self(
class: Json::safeString($throwable::class),
message: $capturePolicy->redactText(Json::safeString($throwable->getMessage())),
code: $code,
file: Json::safeString($throwable->getFile()),
line: $throwable->getLine(),
trace: $trace,
toString: $capturePolicy->redactText(Json::safeString($toString)),
previous: $throwable->getPrevious() !== null ? self::fromThrowable($throwable->getPrevious()) : null,
);
}
/**
* Returns the captured throwable class.
*
* @return string Captured throwable class.
*/
public function getClass(): string
{
return $this->class;
}
/**
* Returns the captured throwable code.
*
* @return int|string Captured throwable code.
*/
public function getCode(): int|string
{
return $this->code;
}
/**
* Returns the file where the throwable originated.
*
* @return string Origin file path.
*/
public function getFile(): string
{
return $this->file;
}
/**
* Returns the line where the throwable originated.
*
* @return int Origin line number.
*/
public function getLine(): int
{
return $this->line;
}
/**
* Returns the captured throwable message.
*
* @return string Captured throwable message.
*/
public function getMessage(): string
{
return $this->message;
}
/**
* Returns the previous throwable snapshot or `null`.
*
* @return self|null Previous throwable snapshot or `null`.
*/
public function getPrevious(): self|null
{
return $this->previous;
}
/**
* Returns the trace frames with their arguments projected to plain display values.
*
* @return list<array<string, mixed>> Display-safe trace frames.
*/
public function getTrace(): array
{
return array_map(
static fn(array $frame): array => array_replace($frame, ['args' => $frame['args']->values()]),
$this->trace,
);
}
/**
* Returns the throwable snapshot for JSON serialization.
*
* @return array<string, mixed> Serialized throwable snapshot.
*/
public function jsonSerialize(): array
{
return [
'class' => $this->class,
'message' => $this->message,
'code' => $this->code,
'file' => $this->file,
'line' => $this->line,
'trace' => array_map(
static fn(array $frame): array => array_replace($frame, ['args' => $frame['args']->jsonSerialize()]),
$this->trace,
),
'toString' => $this->toString,
'previous' => $this->previous?->jsonSerialize(),
];
}
}