forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathError.ts
More file actions
70 lines (62 loc) · 2.01 KB
/
Error.ts
File metadata and controls
70 lines (62 loc) · 2.01 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
interface ErrorType {
name: string;
new (...args: any[]): Error;
}
function __TS__GetErrorStack(constructor: () => any): string {
let level = 1;
while (true) {
const info = debug.getinfo(level, "f");
level += 1;
if (!info) {
// constructor is not in call stack
level = 1;
break;
} else if (info.func === constructor) {
break;
}
}
return debug.traceback(undefined, level);
}
function __TS__WrapErrorToString<T extends Error>(getDescription: (this: T) => string): (this: T) => string {
return function (this: Error): string {
const description = getDescription.call(this);
const caller = debug.getinfo(3, "f");
if (_VERSION === "Lua 5.1" || (caller && caller.func !== error)) {
return description;
} else {
return `${description}\n${this.stack}`;
}
};
}
function __TS__InitErrorClass(Type: ErrorType, name: string): any {
Type.name = name;
return setmetatable(Type, {
__call: (_self: any, message: string) => new Type(message),
});
}
Error = __TS__InitErrorClass(
class implements Error {
public name = "Error";
public stack: string;
constructor(public message = "") {
this.stack = __TS__GetErrorStack((this.constructor as any).new);
const metatable = getmetatable(this);
if (!metatable.__errorToStringPatched) {
metatable.__errorToStringPatched = true;
metatable.__tostring = __TS__WrapErrorToString(metatable.__tostring);
}
}
public toString(): string {
return this.message !== "" ? `${this.name}: ${this.message}` : this.name;
}
},
"Error"
);
for (const errorName of ["RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError"]) {
globalThis[errorName] = __TS__InitErrorClass(
class extends Error {
public name = errorName;
},
errorName
);
}