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

Commit

Permalink
fix(error): fix #618, ZoneAwareError should copy Error's static prope…
Browse files Browse the repository at this point in the history
…ties (#647)
  • Loading branch information
JiaLiPassion authored and mhevery committed Mar 8, 2017
1 parent 2594940 commit 2d30914
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions karma-build.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
module.exports = function (config) {
require('./karma-base.conf.js')(config);
config.files.push('build/test/wtf_mock.js');
config.files.push('build/test/custom_error.js');
config.files.push('build/lib/zone.js');
config.files.push('build/test/main.js');
};
1 change: 1 addition & 0 deletions karma-dist.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
module.exports = function (config) {
require('./karma-base.conf.js')(config);
config.files.push('build/test/wtf_mock.js');
config.files.push('build/test/custom_error.js');
config.files.push('dist/zone.js');
config.files.push('dist/async-test.js');
config.files.push('dist/fake-async-test.js');
Expand Down
19 changes: 19 additions & 0 deletions lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,25 @@ const Zone: ZoneType = (function(global: any) {
ZoneAwareError[Zone.__symbol__('blacklistedStackFrames')] = blackListedStackFrames;
ZoneAwareError[stackRewrite] = false;

// those properties need special handling
const specialPropertyNames = ['stackTraceLimit', 'captureStackTrace', 'prepareStackTrace'];
// those properties of NativeError should be set to ZoneAwareError
const nativeErrorProperties = Object.keys(NativeError);
if (nativeErrorProperties) {
nativeErrorProperties.forEach(prop => {
if (specialPropertyNames.filter(sp => sp === prop).length === 0) {
Object.defineProperty(ZoneAwareError, prop, {
get: function() {
return NativeError[prop];
},
set: function(value) {
NativeError[prop] = value;
}
});
}
});
}

if (NativeError.hasOwnProperty('stackTraceLimit')) {
// Extend default stack limit as we will be removing few frames.
NativeError.stackTraceLimit = Math.max(NativeError.stackTraceLimit, 15);
Expand Down
12 changes: 12 additions & 0 deletions test/common/Error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ describe('ZoneAwareError', () => {
expect(error1.message).toEqual('test new error message');
});

it('should copy customized NativeError properties to ZoneAwareError', () => {
const spy = jasmine.createSpy('errorCustomFunction');
const NativeError = global[Zone['__symbol__']('Error')];
NativeError.customFunction = function(args) {
spy(args);
};
expect(Error['customProperty']).toBe('customProperty');
expect(typeof Error['customFunction']).toBe('function');
Error['customFunction']('test');
expect(spy).toHaveBeenCalledWith('test');
});

it('should show zone names in stack frames and remove extra frames', () => {
const rootZone = getRootZone();
const innerZone = rootZone.fork({name: 'InnerZone'});
Expand Down
14 changes: 14 additions & 0 deletions test/custom_error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

'use strict';
(function(global) {
const NativeError = global['Error'];
NativeError.customProperty = 'customProperty';
NativeError.customFunction = function() {};
})(typeof window === 'object' && window || typeof self === 'object' && self || global);
1 change: 1 addition & 0 deletions test/node_entry_point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

// Must be loaded before zone loads, so that zone can detect WTF.
import './wtf_mock';
import './custom_error';

// Setup tests for Zone without microtask support
import '../lib/zone';
Expand Down

0 comments on commit 2d30914

Please sign in to comment.