@@ -10,17 +10,10 @@ function isErrorStackTraceLimitWritable() {
1010 : desc . set !== undefined ;
1111}
1212
13- class ErrorWithStack extends Error {
14- constructor ( ) {
15- super ( ) ;
16- this . name = "ErrorWithStack" ;
17- }
18- }
19-
2013/**
2114 * Hide internal stack frames from the error stack trace.
2215 */
23- function hideInternalStackFrames ( stack : string ) : string {
16+ export function hideInternalStackFrames ( stack : string ) : string {
2417 const lines = stack . split ( "\n at " ) ;
2518 if ( lines . length <= 1 ) {
2619 return stack ;
@@ -30,14 +23,17 @@ function hideInternalStackFrames(stack: string): string {
3023}
3124
3225// https://github.com/nodejs/node/blob/360f7cc7867b43344aac00564286b895e15f21d7/lib/internal/errors.js#L411-L432
33- function makeErrorForHideStackFrame < B extends new ( ...args : any [ ] ) => Error > (
26+ /**
27+ * Creates a custom error class that hides stack frames.
28+ */
29+ export function makeErrorForHideStackFrame < B extends new ( ...args : any [ ] ) => Error > (
3430 Base : B ,
3531 clazz : any ,
3632) : {
37- new ( ...args : ConstructorParameters < B > ) : InstanceType < B > & { errorWithStack : ErrorWithStack } ;
33+ new ( ...args : ConstructorParameters < B > ) : InstanceType < B > & { errorStack : string | undefined } ;
3834} {
3935 class HideStackFramesError extends Base {
40- #errorWithStack: ErrorWithStack ;
36+ #hiddenStack: string | undefined ;
4137
4238 constructor ( ...args : any [ ] ) {
4339 if ( isErrorStackTraceLimitWritable ( ) ) {
@@ -48,13 +44,15 @@ function makeErrorForHideStackFrame<B extends new (...args: any[]) => Error>(
4844 } else {
4945 super ( ...args ) ;
5046 }
51- this . #errorWithStack = new ErrorWithStack ( ) ;
52- this . #errorWithStack. stack = hideInternalStackFrames ( this . #errorWithStack. stack ?? "" ) ;
47+ const stack = new Error ( ) . stack ;
48+ if ( stack ) {
49+ this . #hiddenStack = hideInternalStackFrames ( stack . replace ( / ^ E r r o r / , this . name ) ) ;
50+ }
5351 }
5452
5553 // use `getter` here to avoid the stack trace being captured by loggers
56- get errorWithStack ( ) {
57- return this . #errorWithStack ;
54+ get errorStack ( ) {
55+ return this . #hiddenStack ;
5856 }
5957
6058 // This is a workaround for wpt tests that expect that the error
0 commit comments