@@ -119,20 +119,27 @@ Zone.__load_patch('Error', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
119119 function ZoneAwareError ( this : unknown | typeof NativeError ) : Error {
120120 // We always have to return native error otherwise the browser console will not work.
121121 let error : Error = NativeError . apply ( this , arguments ) ;
122- // Save original stack trace
123- const originalStack = ( error as any ) [ 'originalStack' ] = error . stack ;
124122
125- // Process the stack trace and rewrite the frames.
126- if ( ( ZoneAwareError as any ) [ stackRewrite ] && originalStack ) {
123+ if ( ( ZoneAwareError as any ) [ stackRewrite ] ) {
127124 let zoneFrame = api . currentZoneFrame ( ) ;
128125 if ( zoneJsInternalStackFramesPolicy === 'lazy' ) {
129- // don't handle stack trace now
126+ // Don't handle stack trace now, and also, don't read the `stack` property. The V8 triggers
127+ // the stack trace serialization only when the `stack` property is read for the first time
128+ // within the `Accessors::ErrorStackGetter`. We want to avoid formatting the stack trace
129+ // when the lazy policy is used, and we don't need the stack until the `zoneAwareStack`
130+ // property is read.
130131 ( error as any ) [ api . symbol ( 'zoneFrameNames' ) ] = buildZoneFrameNames ( zoneFrame ) ;
131132 } else if ( zoneJsInternalStackFramesPolicy === 'default' ) {
132- try {
133- error . stack = error . zoneAwareStack = buildZoneAwareStackFrames ( originalStack , zoneFrame ) ;
134- } catch ( e ) {
135- // ignore as some browsers don't allow overriding of stack
133+ // Note: we access the `error.stack` property immediately and save it as original stack
134+ // trace only when the default policy is used.
135+ const originalStack = error . originalStack = error . stack ;
136+ if ( originalStack ) {
137+ try {
138+ error . stack = error . zoneAwareStack =
139+ buildZoneAwareStackFrames ( originalStack , zoneFrame ) ;
140+ } catch ( e ) {
141+ // ignore as some browsers don't allow overriding of stack
142+ }
136143 }
137144 }
138145 }
@@ -162,22 +169,26 @@ Zone.__load_patch('Error', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
162169
163170 const zoneAwareStackSymbol = api . symbol ( 'zoneAwareStack' ) ;
164171
165- // try to define zoneAwareStack property when zoneJsInternal frames policy is delay
172+ // try to define zoneAwareStack property when zoneJsInternal frames policy is lazy
166173 if ( zoneJsInternalStackFramesPolicy === 'lazy' ) {
167174 Object . defineProperty ( ZoneAwareError . prototype , 'zoneAwareStack' , {
168175 configurable : true ,
169176 enumerable : true ,
170177 get : function ( ) {
171178 if ( ! this [ zoneAwareStackSymbol ] ) {
172179 this [ zoneAwareStackSymbol ] = buildZoneAwareStackFrames (
173- this . originalStack , this [ api . symbol ( 'zoneFrameNames' ) ] , false ) ;
180+ // Note: the `zoneAwareStack` may be overwritten since it has a setter and it'll
181+ // contain the latest value, otherwise we just fallback to the `stack` property.
182+ // Reading `this.stack` will trigger stack trace serialization.
183+ this . originalStack ?? this . stack , this [ api . symbol ( 'zoneFrameNames' ) ] , false ) ;
174184 }
175185 return this [ zoneAwareStackSymbol ] ;
176186 } ,
177187 set : function ( newStack : string ) {
188+ // Note: do not set the `stack` property as some browsers don't support overriding it.
178189 this . originalStack = newStack ;
179- this [ zoneAwareStackSymbol ] = buildZoneAwareStackFrames (
180- this . originalStack , this [ api . symbol ( 'zoneFrameNames' ) ] , false ) ;
190+ this [ zoneAwareStackSymbol ] =
191+ buildZoneAwareStackFrames ( newStack , this [ api . symbol ( 'zoneFrameNames' ) ] , false ) ;
181192 }
182193 } ) ;
183194 }
0 commit comments