|
| 1 | +Error = class { |
| 2 | + public static captureStackTrace(targetObject: {}, depth: number): void { |
| 3 | + targetObject["stack"] = debug.traceback(undefined, depth); |
| 4 | + } |
| 5 | + |
| 6 | + public message: string; |
| 7 | + public name: string; |
| 8 | + public stack?: string; |
| 9 | + |
| 10 | + constructor(message?: string, name?: string) { |
| 11 | + this.message = message || ""; |
| 12 | + this.name = name || "Error"; |
| 13 | + Error["captureStackTrace"](this, name ? 5 : 4); |
| 14 | + } |
| 15 | + |
| 16 | + public __tostring(): string { |
| 17 | + return `${this.name}: ${this.message}`; |
| 18 | + } |
| 19 | +} as any; |
| 20 | + |
| 21 | +setmetatable(Error, { __call: (_self: any, message: string) => new Error(message) }); |
| 22 | + |
| 23 | +/** Standard error types */ |
| 24 | + |
| 25 | +RangeError = class extends Error { |
| 26 | + constructor(message?: string) { |
| 27 | + // @ts-ignore |
| 28 | + super(message, "RangeError"); |
| 29 | + } |
| 30 | + public __tostring(): string { |
| 31 | + return `${this.name}: ${this.message}`; |
| 32 | + } |
| 33 | +} as any; |
| 34 | +setmetatable(RangeError, { |
| 35 | + __call: (_self: any, message: string) => new RangeError(message), |
| 36 | + __index: getmetatable(RangeError), |
| 37 | +}); |
| 38 | + |
| 39 | +ReferenceError = class extends Error { |
| 40 | + constructor(message?: string) { |
| 41 | + // @ts-ignore |
| 42 | + super(message, "ReferenceError"); |
| 43 | + } |
| 44 | + public __tostring(): string { |
| 45 | + return `${this.name}: ${this.message}`; |
| 46 | + } |
| 47 | +} as any; |
| 48 | +setmetatable(ReferenceError, { |
| 49 | + __call: (_self: any, message: string) => new ReferenceError(message), |
| 50 | + __index: getmetatable(ReferenceError), |
| 51 | +}); |
| 52 | + |
| 53 | +SyntaxError = class extends Error { |
| 54 | + constructor(message?: string) { |
| 55 | + // @ts-ignore |
| 56 | + super(message, "SyntaxError"); |
| 57 | + } |
| 58 | + public __tostring(): string { |
| 59 | + return `${this.name}: ${this.message}`; |
| 60 | + } |
| 61 | +} as any; |
| 62 | +setmetatable(SyntaxError, { |
| 63 | + __call: (_self: any, message: string) => new SyntaxError(message), |
| 64 | + __index: getmetatable(SyntaxError), |
| 65 | +}); |
| 66 | + |
| 67 | +TypeError = class extends Error { |
| 68 | + constructor(message?: string) { |
| 69 | + // @ts-ignore |
| 70 | + super(message, "TypeError"); |
| 71 | + } |
| 72 | + public __tostring(): string { |
| 73 | + return `${this.name}: ${this.message}`; |
| 74 | + } |
| 75 | +} as any; |
| 76 | +setmetatable(TypeError, { |
| 77 | + __call: (_self: any, message: string) => new TypeError(message), |
| 78 | + __index: getmetatable(TypeError), |
| 79 | +}); |
| 80 | + |
| 81 | +URIError = class extends Error { |
| 82 | + constructor(message?: string) { |
| 83 | + // @ts-ignore |
| 84 | + super(message, "URIError"); |
| 85 | + } |
| 86 | + public __tostring(): string { |
| 87 | + return `${this.name}: ${this.message}`; |
| 88 | + } |
| 89 | +} as any; |
| 90 | +setmetatable(URIError, { |
| 91 | + __call: (_self: any, message: string) => new URIError(message), |
| 92 | + __index: getmetatable(URIError), |
| 93 | +}); |
0 commit comments