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

Commit 2d30914

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(error): fix #618, ZoneAwareError should copy Error's static propeties (#647)
1 parent 2594940 commit 2d30914

File tree

6 files changed

+48
-0
lines changed

6 files changed

+48
-0
lines changed

Diff for: karma-build.conf.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
module.exports = function (config) {
1010
require('./karma-base.conf.js')(config);
1111
config.files.push('build/test/wtf_mock.js');
12+
config.files.push('build/test/custom_error.js');
1213
config.files.push('build/lib/zone.js');
1314
config.files.push('build/test/main.js');
1415
};

Diff for: karma-dist.conf.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
module.exports = function (config) {
1010
require('./karma-base.conf.js')(config);
1111
config.files.push('build/test/wtf_mock.js');
12+
config.files.push('build/test/custom_error.js');
1213
config.files.push('dist/zone.js');
1314
config.files.push('dist/async-test.js');
1415
config.files.push('dist/fake-async-test.js');

Diff for: lib/zone.ts

+19
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,25 @@ const Zone: ZoneType = (function(global: any) {
17441744
ZoneAwareError[Zone.__symbol__('blacklistedStackFrames')] = blackListedStackFrames;
17451745
ZoneAwareError[stackRewrite] = false;
17461746

1747+
// those properties need special handling
1748+
const specialPropertyNames = ['stackTraceLimit', 'captureStackTrace', 'prepareStackTrace'];
1749+
// those properties of NativeError should be set to ZoneAwareError
1750+
const nativeErrorProperties = Object.keys(NativeError);
1751+
if (nativeErrorProperties) {
1752+
nativeErrorProperties.forEach(prop => {
1753+
if (specialPropertyNames.filter(sp => sp === prop).length === 0) {
1754+
Object.defineProperty(ZoneAwareError, prop, {
1755+
get: function() {
1756+
return NativeError[prop];
1757+
},
1758+
set: function(value) {
1759+
NativeError[prop] = value;
1760+
}
1761+
});
1762+
}
1763+
});
1764+
}
1765+
17471766
if (NativeError.hasOwnProperty('stackTraceLimit')) {
17481767
// Extend default stack limit as we will be removing few frames.
17491768
NativeError.stackTraceLimit = Math.max(NativeError.stackTraceLimit, 15);

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

+12
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ describe('ZoneAwareError', () => {
157157
expect(error1.message).toEqual('test new error message');
158158
});
159159

160+
it('should copy customized NativeError properties to ZoneAwareError', () => {
161+
const spy = jasmine.createSpy('errorCustomFunction');
162+
const NativeError = global[Zone['__symbol__']('Error')];
163+
NativeError.customFunction = function(args) {
164+
spy(args);
165+
};
166+
expect(Error['customProperty']).toBe('customProperty');
167+
expect(typeof Error['customFunction']).toBe('function');
168+
Error['customFunction']('test');
169+
expect(spy).toHaveBeenCalledWith('test');
170+
});
171+
160172
it('should show zone names in stack frames and remove extra frames', () => {
161173
const rootZone = getRootZone();
162174
const innerZone = rootZone.fork({name: 'InnerZone'});

Diff for: test/custom_error.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
'use strict';
10+
(function(global) {
11+
const NativeError = global['Error'];
12+
NativeError.customProperty = 'customProperty';
13+
NativeError.customFunction = function() {};
14+
})(typeof window === 'object' && window || typeof self === 'object' && self || global);

Diff for: test/node_entry_point.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
// Must be loaded before zone loads, so that zone can detect WTF.
1010
import './wtf_mock';
11+
import './custom_error';
1112

1213
// Setup tests for Zone without microtask support
1314
import '../lib/zone';

0 commit comments

Comments
 (0)