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

fix #546, should keep error prototype chain correctly #547

Merged
merged 2 commits into from
Dec 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1283,13 +1283,14 @@ const Zone: ZoneType = (function(global: any) {
function ZoneAwareError() {
// Create an Error.
let error: Error = NativeError.apply(this, arguments);
this.message = error.message;

// Save original stack trace
error.originalStack = error.stack;
this.originalStack = error.stack;

// Process the stack trace and rewrite the frames.
if (ZoneAwareError[stackRewrite] && error.originalStack) {
let frames: string[] = error.originalStack.split('\n');
if (ZoneAwareError[stackRewrite] && this.originalStack) {
let frames: string[] = this.originalStack.split('\n');
let zoneFrame = _currentZoneFrame;
let i = 0;
// Find the first frame
Expand Down Expand Up @@ -1317,13 +1318,12 @@ const Zone: ZoneType = (function(global: any) {
}
}
}
error.stack = error.zoneAwareStack = frames.join('\n');
this.stack = this.zoneAwareStack = frames.join('\n');
}
return error;
}
};

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

Expand Down
8 changes: 8 additions & 0 deletions test/common/Error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ describe('ZoneAwareError', () => {
// and there is no point in running them.
if (!Error['stackRewrite']) return;

it('should keep error prototype chain correctly', () => {
class MyError extends Error {}
const myError = new MyError();
expect(myError instanceof Error).toBe(true);
expect(myError instanceof MyError).toBe(true);
expect(myError.stack).not.toBe(undefined);
});

it('should show zone names in stack frames and remove extra frames', () => {
const rootZone = getRootZone();
const innerZone = rootZone.fork({name: 'InnerZone'});
Expand Down