Repository navigation
Expand file tree
/
Copy pathPHPError.js
More file actions
297 lines (297 loc) · 12.5 KB
/
Copy pathPHPError.js
File metadata and controls
297 lines (297 loc) · 12.5 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
287
288
289
290
291
292
293
294
295
296
297
import { PHPReference } from "./PHPVariable.js";
import { SYMBOL_PHP_NAME, SYMBOL_PHP_INIT, SYMBOL_PHP_NEW } from "./Reflection.js";
export class PHPError extends Error {
static E_ERROR = 1;
static E_WARNING = 2;
static E_PARSE = 4;
static E_NOTICE = 8;
static E_CORE_ERROR = 16;
static E_CORE_WARNING = 32;
static E_COMPILE_ERROR = 64;
static E_COMPILE_WARNING = 128;
static E_USER_ERROR = 256;
static E_USER_WARNING = 512;
static E_USER_NOTICE = 1024;
static E_STRICT = 2048;
static E_RECOVERABLE_ERROR = 4096;
static E_DEPRECATED = 8192;
static E_USER_DEPRECATED = 16384;
static E_ALL = 32767;
phpCode;
phpFile;
phpLine;
phpTrace;
previous;
rawJSStack = "";
constructor(messageArg = "", codeArg = 0, fileArg = "", lineArg = 0, traceArg = [], previousArg = null) {
const message = String(messageArg ?? "");
const code = Number(codeArg || 0);
const file = String(fileArg || "");
const line = Number(lineArg || 0);
const trace = traceArg || [];
const previous = previousArg ?? null;
super(message);
this.name = this.constructor.name;
this.phpCode = code;
this.phpFile = file;
this.phpLine = line;
this.phpTrace = trace;
this.previous = previous;
this.rawJSStack = this.stack || "";
if (this.stack) {
this.stack = PHPError.virtualizeJSStack(this.rawJSStack, file, line, trace);
}
}
static wrapJSError(err) {
if (err instanceof PHPError) {
return err;
}
if (err && typeof err === "object" && typeof err.message === "string") {
if (err.name === "TypeError") {
return new PHPTypeError(err.message);
}
return new PHPFatalError(err.message);
}
return new PHPError(String(err ?? "Unknown error"));
}
static async [SYMBOL_PHP_NEW](ctx, messageArg, codeArg, previousArg) {
const obj = Object.create(this.prototype);
await this[SYMBOL_PHP_INIT](ctx, obj);
await obj.__construct(ctx, messageArg, codeArg, previousArg);
if (obj.rawJSStack) {
obj.stack = PHPError.virtualizeJSStack(obj.rawJSStack, obj.phpFile, obj.phpLine, obj.phpTrace);
}
return obj;
}
static async [SYMBOL_PHP_INIT](ctx, obj) {
obj.name = this.name;
obj.phpCode = 0;
obj.phpFile = ctx?.currentFile || "";
obj.phpLine = ctx?.currentLine || 0;
obj.phpTrace = ctx?.getPHPBacktrace ? ctx.getPHPBacktrace() : [];
obj.previous = null;
obj.rawJSStack = new Error().stack || "";
}
async __construct(ctx, messageArg, codeArg, previousArg) {
const msg = messageArg && typeof messageArg.get === "function" ? await messageArg.get() : messageArg ?? "";
const code = codeArg && typeof codeArg.get === "function" ? await codeArg.get() : codeArg ?? 0;
const previous = previousArg && typeof previousArg.get === "function" ? await previousArg.get() : previousArg ?? null;
this.message = String(msg ?? "");
this.phpCode = Number(code || 0);
this.previous = previous;
}
getMessage() {
return this.message || this.properties?.message || "";
}
getmessage() { return this.getMessage(); }
getCode() {
return this.phpCode || this.properties?.code || 0;
}
getcode() { return this.getCode(); }
getFile() {
return this.phpFile || this.properties?.file || "";
}
getfile() { return this.getFile(); }
getLine() {
return this.phpLine || this.properties?.line || 0;
}
getline() { return this.getLine(); }
getPrevious() {
return this.previous || this.properties?.previous || null;
}
getprevious() { return this.getPrevious(); }
gettrace() { return this.phpTrace || []; }
gettraceasstring() { return this.getPHPStackTraceString(); }
static virtualizeJSStack(jsStack, phpFile = "", phpLine = 0, phpTrace = []) {
const rawLines = (jsStack || "").split("\n");
let header = rawLines[0] || "PHP Error";
header = header.replace(/^PHPFatalError:/, "PHP Fatal Error:");
const formattedFrames = [];
if (phpTrace && phpTrace.length > 0) {
phpTrace.forEach((frame, idx) => {
const fileLoc = `${frame.file || ""}:${frame.line || 0}`;
const funcStr = frame.class
? `${frame.class}${frame.type || "->"}${frame.function || "main"}`
: frame.function || "{main}";
formattedFrames.push(` #${idx} ${fileLoc}: ${funcStr}()`);
});
return `${header}\nPHP Stack Trace:\n${formattedFrames.join("\n")}`;
}
let frameIdx = 0;
for (let i = 1; i < rawLines.length; i++) {
const line = rawLines[i].trim();
if (!line)
continue;
if (line.includes("PHPContext.") || line.includes("PHPContext.ts") || line.includes("PHPContext.js") || line.includes("PHPEngine.") || line.includes("php-http-server") || line.includes("node:internal"))
continue;
const matchPhp = line.match(/\((.*?\.php):(\d+):(\d+)\)/) || line.match(/at\s+(.*?\.php):(\d+):(\d+)/) || line.match(/at\s+([^\s]+)\s+\((.*?):(\d+):(\d+)\)/);
if (matchPhp) {
const file = matchPhp[2] || matchPhp[1];
const lineNum = matchPhp[3] || matchPhp[2];
const jsFunc = matchPhp[1] && matchPhp[2] ? matchPhp[1] : "{main}";
formattedFrames.push(` #${frameIdx++} ${file}:${lineNum}: ${jsFunc}()`);
continue;
}
}
if (formattedFrames.length === 0) {
formattedFrames.push(` #0 ${phpFile || ""}:${phpLine || 0}: {main}()`);
}
return `${header}\nPHP Stack Trace:\n${formattedFrames.join("\n")}`;
}
getPHPStackTraceString() {
return PHPError.virtualizeJSStack(this.rawJSStack || this.stack || "", this.phpFile, this.phpLine, this.phpTrace);
}
}
export class PHPException extends PHPError {
static [SYMBOL_PHP_NAME] = "Exception";
constructor(messageArg = "", codeArg = 0, previousArg = null) {
super(messageArg, codeArg, "", 0, [], previousArg);
}
}
export class InvalidArgumentException extends PHPException {
static [SYMBOL_PHP_NAME] = "InvalidArgumentException";
}
export class BadMethodCallException extends PHPException {
static [SYMBOL_PHP_NAME] = "BadMethodCallException";
}
export class DomainException extends PHPException {
static [SYMBOL_PHP_NAME] = "DomainException";
}
export class LengthException extends PHPException {
static [SYMBOL_PHP_NAME] = "LengthException";
}
export class LogicException extends PHPException {
static [SYMBOL_PHP_NAME] = "LogicException";
}
export class OutOfRangeException extends PHPException {
static [SYMBOL_PHP_NAME] = "OutOfRangeException";
}
export class OverflowException extends PHPException {
static [SYMBOL_PHP_NAME] = "OverflowException";
}
export class RangeException extends PHPException {
static [SYMBOL_PHP_NAME] = "RangeException";
}
export class RuntimeException extends PHPException {
static [SYMBOL_PHP_NAME] = "RuntimeException";
}
export class UnderflowException extends PHPException {
static [SYMBOL_PHP_NAME] = "UnderflowException";
}
export class UnexpectedValueException extends PHPException {
static [SYMBOL_PHP_NAME] = "UnexpectedValueException";
}
export class ErrorException extends PHPError {
static [SYMBOL_PHP_NAME] = "ErrorException";
severity = 1;
constructor(message = "", code = 0, severity = 1, file = "", line = 0, previous = null) {
super(message, code, file, line, [], previous);
this.severity = severity;
}
async __construct(ctx, messageArg, codeArg, severityArg, fileArg, lineArg, previousArg) {
const msg = messageArg ? (typeof messageArg.get === "function" ? await messageArg.get() : messageArg) : "";
const code = codeArg ? (typeof codeArg.get === "function" ? await codeArg.get() : codeArg) : 0;
const severity = severityArg ? (typeof severityArg.get === "function" ? await severityArg.get() : severityArg) : 1;
const file = fileArg ? (typeof fileArg.get === "function" ? await fileArg.get() : fileArg) : (ctx?.currentFile || "");
const line = lineArg ? (typeof lineArg.get === "function" ? await lineArg.get() : lineArg) : (ctx?.currentLine || 0);
const previous = previousArg ? (typeof previousArg.get === "function" ? await previousArg.get() : previousArg) : null;
this.message = String(msg ?? "");
this.phpCode = Number(code || 0);
this.severity = Number(severity || 1);
this.phpFile = String(file || "");
this.phpLine = Number(line || 0);
this.previous = previous;
}
getSeverity() { return this.severity; }
getseverity() { return this.getSeverity(); }
}
export class PHPTypeError extends PHPError {
static [SYMBOL_PHP_NAME] = "TypeError";
}
export class PHPValueError extends PHPError {
static [SYMBOL_PHP_NAME] = "ValueError";
}
export class PHPParseError extends PHPError {
static [SYMBOL_PHP_NAME] = "ParseError";
}
export class PHPFatalError extends PHPError {
static [SYMBOL_PHP_NAME] = "Error";
}
export class PHPNotice extends PHPError {
static [SYMBOL_PHP_NAME] = "Notice";
}
export class PHPWarning extends PHPError {
static [SYMBOL_PHP_NAME] = "Warning";
}
export class PHPExit extends PHPError {
status;
constructor(status = 0) {
super(`PHP Exit with status ${status}`);
this.status = status;
}
}
export class ErrorRuntime {
static async debug_backtrace(ctx) {
return ctx.getPHPBacktrace();
}
static async debug_print_backtrace(ctx) {
const trace = ctx.getPHPBacktrace().map((frame, index) => `#${index} ${frame.file || __filename}(${frame.line || 0}): ${frame.function || "{main}"}()\n`).join("");
await ctx.echo(trace);
return trace;
}
static async set_error_handler(ctx, handlerArg, levelsArg = PHPError.E_ALL) {
const handler = handlerArg instanceof PHPReference ? await handlerArg.get() : handlerArg;
const levels = levelsArg instanceof PHPReference ? await levelsArg.get() : levelsArg;
return ctx.setErrorHandler(handler, levels !== undefined ? Number(levels) : PHPError.E_ALL);
}
static async restore_error_handler(ctx) {
return ctx.restoreErrorHandler();
}
static async trigger_error(ctx, messageArg, levelArg = PHPError.E_USER_NOTICE) {
const message = messageArg instanceof PHPReference ? await messageArg.get() : messageArg;
const level = levelArg instanceof PHPReference ? await levelArg.get() : levelArg;
return await ctx.triggerError(String(message ?? ""), level !== undefined ? Number(level) : PHPError.E_USER_NOTICE);
}
static async error_reporting(ctx, levelArg) {
const level = levelArg instanceof PHPReference ? await levelArg.get() : levelArg;
const previous = ctx.errorReportingLevel;
if (level !== undefined && level !== null)
ctx.errorReportingLevel = Number(level);
return previous;
}
static functions = {
"debug_backtrace": ErrorRuntime.debug_backtrace,
"debug_print_backtrace": ErrorRuntime.debug_print_backtrace,
"set_error_handler": ErrorRuntime.set_error_handler,
"restore_error_handler": ErrorRuntime.restore_error_handler,
"trigger_error": ErrorRuntime.trigger_error,
"user_error": ErrorRuntime.trigger_error,
"error_reporting": ErrorRuntime.error_reporting,
};
static classes = {
"exception": PHPException,
"errorexception": ErrorException,
"invalidargumentexception": PHPException,
"badmethodcallexception": PHPException,
"domainexception": PHPException,
"lengthexception": PHPException,
"logicexception": PHPException,
"outofrangeexception": PHPException,
"overflowexception": PHPException,
"rangeexception": PHPException,
"runtimeexception": PHPException,
"underflowexception": PHPException,
"unexpectedvalueexception": PHPException,
"error": PHPError,
"typeerror": PHPTypeError,
"parseerror": PHPParseError,
"fatalerror": PHPFatalError,
"warning": PHPWarning,
"notice": PHPNotice,
};
static register(engine) {
engine.registerFunctions(ErrorRuntime.functions);
engine.registerClasses(ErrorRuntime.classes);
}
}
//# sourceMappingURL=PHPError.js.map