@@ -20,14 +20,16 @@ function __TS__GetErrorStack(constructor: Function): string {
2020 return debug . traceback ( undefined , level ) ;
2121}
2222
23- function __TS__GetErrorString ( this : void , errorObj : Error ) : string {
24- const description = errorObj . message !== "" ? `${ errorObj . name } : ${ errorObj . message } ` : errorObj . name ;
25- const caller = debug . getinfo ( 3 , "f" ) ;
26- if ( _VERSION === "Lua 5.1" || ( caller && caller . func !== error ) ) {
27- return description ;
28- } else {
29- return `${ description } \n${ errorObj . stack } ` ;
30- }
23+ function __TS__WrapErrorToString < T extends Error > ( getDescription : ( this : T ) => string ) : ( this : T ) => string {
24+ return function ( this : Error ) : string {
25+ const description = getDescription . call ( this ) ;
26+ const caller = debug . getinfo ( 3 , "f" ) ;
27+ if ( _VERSION === "Lua 5.1" || ( caller && caller . func !== error ) ) {
28+ return description ;
29+ } else {
30+ return `${ description } \n${ this . stack } ` ;
31+ }
32+ } ;
3133}
3234
3335function __TS__InitErrorClass < T > ( Type : ErrorType < T > , name : string ) : any {
@@ -44,10 +46,15 @@ Error = __TS__InitErrorClass(
4446
4547 constructor ( public message = "" ) {
4648 this . stack = __TS__GetErrorStack ( ( this . constructor as any ) . new ) ;
49+ const metatable = getmetatable ( this ) ;
50+ if ( ! metatable . __errorToStringPatched ) {
51+ metatable . __errorToStringPatched = true ;
52+ metatable . __tostring = __TS__WrapErrorToString ( metatable . __tostring ) ;
53+ }
4754 }
4855
4956 public toString ( ) : string {
50- return __TS__GetErrorString ( this ) ;
57+ return this . message !== "" ? ` ${ this . name } : ${ this . message } ` : this . name ;
5158 }
5259 } ,
5360 "Error"
@@ -57,10 +64,6 @@ for (const errorName of ["RangeError", "ReferenceError", "SyntaxError", "TypeErr
5764 globalThis [ errorName ] = __TS__InitErrorClass (
5865 class extends Error {
5966 public name = errorName ;
60-
61- public toString ( ) : string {
62- return __TS__GetErrorString ( this ) ;
63- }
6467 } ,
6568 errorName
6669 ) ;
0 commit comments