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

Commit ba7858c

Browse files
JiaLiPassionmhevery
authored andcommitted
fix: Correct ZoneAwareError prototype chain
fix #546, should keep error prototype chain correctly (#547)
1 parent 1bff7c0 commit ba7858c

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

Diff for: lib/zone.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1312,13 +1312,14 @@ const Zone: ZoneType = (function(global: any) {
13121312
function ZoneAwareError() {
13131313
// Create an Error.
13141314
let error: Error = NativeError.apply(this, arguments);
1315+
this.message = error.message;
13151316

13161317
// Save original stack trace
1317-
error.originalStack = error.stack;
1318+
this.originalStack = error.stack;
13181319

13191320
// Process the stack trace and rewrite the frames.
1320-
if (ZoneAwareError[stackRewrite] && error.originalStack) {
1321-
let frames: string[] = error.originalStack.split('\n');
1321+
if (ZoneAwareError[stackRewrite] && this.originalStack) {
1322+
let frames: string[] = this.originalStack.split('\n');
13221323
let zoneFrame = _currentZoneFrame;
13231324
let i = 0;
13241325
// Find the first frame
@@ -1346,13 +1347,12 @@ const Zone: ZoneType = (function(global: any) {
13461347
}
13471348
}
13481349
}
1349-
error.stack = error.zoneAwareStack = frames.join('\n');
1350+
this.stack = this.zoneAwareStack = frames.join('\n');
13501351
}
1351-
return error;
1352-
}
1352+
};
13531353

13541354
// Copy the prototype so that instanceof operator works as expected
1355-
ZoneAwareError.prototype = NativeError.prototype;
1355+
ZoneAwareError.prototype = Object.create(NativeError.prototype);
13561356
ZoneAwareError[Zone.__symbol__('blacklistedStackFrames')] = blackListedStackFrames;
13571357
ZoneAwareError[stackRewrite] = false;
13581358

Diff for: test/common/Error.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ describe('ZoneAwareError', () => {
1111
// and there is no point in running them.
1212
if (!Error['stackRewrite']) return;
1313

14+
it('should keep error prototype chain correctly', () => {
15+
class MyError extends Error {}
16+
const myError = new MyError();
17+
expect(myError instanceof Error).toBe(true);
18+
expect(myError instanceof MyError).toBe(true);
19+
expect(myError.stack).not.toBe(undefined);
20+
});
21+
1422
it('should show zone names in stack frames and remove extra frames', () => {
1523
const rootZone = getRootZone();
1624
const innerZone = rootZone.fork({name: 'InnerZone'});

0 commit comments

Comments
 (0)