Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 41f5306

Browse files
JiaLiPassionmhevery
authored andcommitted
fix #574, captureStackTrace will have additional stackframe from Zone will break binding.js (#575)
1 parent 62f1449 commit 41f5306

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/zone.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,9 @@ const Zone: ZoneType = (function(global: any) {
14231423

14241424
if (NativeError.hasOwnProperty('captureStackTrace')) {
14251425
Object.defineProperty(ZoneAwareError, 'captureStackTrace', {
1426-
value: function(targetObject: Object, constructorOpt?: Function) {
1426+
// add named function here because we need to remove this
1427+
// stack frame when prepareStackTrace below
1428+
value: function zoneCaptureStackTrace(targetObject: Object, constructorOpt?: Function) {
14271429
NativeError.captureStackTrace(targetObject, constructorOpt);
14281430
}
14291431
});
@@ -1434,7 +1436,23 @@ const Zone: ZoneType = (function(global: any) {
14341436
return NativeError.prepareStackTrace;
14351437
},
14361438
set: function(value) {
1437-
return NativeError.prepareStackTrace = value;
1439+
if (!value || typeof value !== 'function') {
1440+
return NativeError.prepareStackTrace = value;
1441+
}
1442+
return NativeError.prepareStackTrace = function(error, structuredStackTrace) {
1443+
// remove additional stack information from ZoneAwareError.captureStackTrace
1444+
if (structuredStackTrace) {
1445+
for (let i = 0; i < structuredStackTrace.length; i++) {
1446+
const st = structuredStackTrace[i];
1447+
// remove the first function which name is value
1448+
if (st.getFunctionName() === 'zoneCaptureStackTrace') {
1449+
structuredStackTrace.splice(i, 1);
1450+
break;
1451+
}
1452+
}
1453+
}
1454+
return value.apply(this, [error, structuredStackTrace]);
1455+
};
14381456
}
14391457
});
14401458

test/node/Error.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,16 @@ describe('ZoneAwareError', () => {
2525
Error.captureStackTrace(obj);
2626
expect(obj.stack[0].getFileName()).not.toBeUndefined();
2727
});
28+
29+
it('should not add additional stacktrace from Zone when use prepareStackTrace', () => {
30+
(<any>Error).prepareStackTrace = function(error, stack) {
31+
return stack;
32+
};
33+
let obj: any = new Object();
34+
Error.captureStackTrace(obj);
35+
expect(obj.stack.length).not.toBe(0);
36+
obj.stack.forEach(function(st) {
37+
expect(st.getFunctionName()).not.toEqual('zoneCaptureStackTrace');
38+
});
39+
});
2840
});

0 commit comments

Comments
 (0)