Skip to content

Commit d17c496

Browse files
committed
feat(error): fix angular#975, can config how to load blacklist zone stack frames
1 parent 0a2f6ff commit d17c496

17 files changed

+400
-154
lines changed

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ script:
3939
- node_modules/.bin/karma start karma-build-sauce-mocha.conf.js --single-run
4040
- node_modules/.bin/karma start karma-dist-sauce-selenium3-jasmine.conf.js --single-run
4141
- node_modules/.bin/karma start karma-build-sauce-selenium3-mocha.conf.js --single-run
42+
- node_modules/.bin/karma start karma-dist-sauce-jasmine3.conf.js --single-run --errorpolicy=disable
43+
- node_modules/.bin/karma start karma-dist-sauce-jasmine3.conf.js --single-run --errorpolicy=lazy
4244
- node_modules/.bin/gulp test/node
4345
- node_modules/.bin/gulp test/node -no-patch-clock
4446
- node_modules/.bin/gulp test/bluebird
47+
- node_modules/.bin/gulp test/node/disableerror
48+
- node_modules/.bin/gulp test/node/lazyerror
4549
- node simple-server.js 2>&1> server.log&
4650
- node ./test/webdriver/test.sauce.js
4751

MODULE.md

+51
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,57 @@ you can do like this.
7676
<script src="../dist/zone.js"></script>
7777
```
7878

79+
- Error
80+
81+
By default, `zone.js/dist/zone-error` will not be loaded for performance concern.
82+
This package will provide following functionality.
83+
84+
1. Error inherit: handle `extend Error` issue.
85+
```
86+
class MyError extends Error {}
87+
const myError = new MyError();
88+
console.log('is MyError instanceof Error', (myError instanceof Error));
89+
```
90+
91+
without `zone-error` patch, the example above will output `false`, with the patch, the reuslt will be `true`.
92+
93+
2. BlacklistZoneStackFrames: remove zone.js stack from `stackTrace`, and add `zone` information. Without this patch, a lot of `zone.js` invocation stack will be shown
94+
in stack frames.
95+
96+
```
97+
at zone.run (polyfill.bundle.js: 3424)
98+
at zoneDelegate.invokeTask (polyfill.bundle.js: 3424)
99+
at zoneDelegate.runTask (polyfill.bundle.js: 3424)
100+
at zone.drainMicroTaskQueue (polyfill.bundle.js: 3424)
101+
at a.b.c (vendor.bundle.js: 12345 <angular>)
102+
at d.e.f (main.bundle.js: 23456)
103+
```
104+
105+
with this patch, those zone frames will be removed,
106+
and the zone information `<angular>/<root>` will be added
107+
108+
```
109+
at a.b.c (vendor.bundle.js: 12345 <angular>)
110+
at d.e.f (main.bundle.js: 23456 <root>)
111+
```
112+
113+
The second feature will slow down the `Error` performance, so `zone.js` provide a flag to let you be able to control the behavior.
114+
The flag is `__Zone_Error_BlacklistedStackFrames_policy`. And the available options is:
115+
116+
1. default: this is the default one, if you load `zone.js/dist/zone-error` without
117+
setting the flag, `default` will be used, and `BlackListStackFrames` will be available
118+
when `new Error()`, you can get a `error.stack` which is `zone stack free`. But this
119+
will slow down `new Error()` a little bit.
120+
121+
2. disable: this will disable `BlackListZoneStackFrame` feature, and if you load
122+
`zone.js/dist/zone-error`, you will only get a `wrapped Error` which can handle
123+
`Error inherit` issue.
124+
125+
3. lazy: this is a feature to let you be able to get `BlackListZoneStackFrame` feature,
126+
but not impact performance. But as a trade off, you can't get the `zone free stack
127+
frames` by access `error.stack`. You can only get it by access `error.zoneAwareStack`.
128+
129+
79130
- Angular(2+)
80131
81132
Angular uses zone.js to manage async operations and decide when to perform change detection. Thus, in Angular,

gulpfile.js

+12
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,18 @@ gulp.task('test/bluebird', ['compile-node'], function(cb) {
444444
nodeTest(specFiles, cb);
445445
});
446446

447+
gulp.task('test/node/disableerror', ['compile-node'], function(cb) {
448+
process.env.errorpolicy = 'disable';
449+
var specFiles = ['build/test/node_error_entry_point.js'];
450+
nodeTest(specFiles, cb);
451+
});
452+
453+
gulp.task('test/node/lazyerror', ['compile-node'], function(cb) {
454+
process.env.errorpolicy = 'lazy';
455+
var specFiles = ['build/test/node_error_entry_point.js'];
456+
nodeTest(specFiles, cb);
457+
});
458+
447459
// Check the coding standards and programming errors
448460
gulp.task('lint', () => {
449461
const tslint = require('gulp-tslint');

karma-base.conf.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
module.exports = function(config) {
1010
config.set({
1111
basePath: '',
12+
client: {errorpolicy: config.errorpolicy},
1213
files: [
1314
'node_modules/systemjs/dist/system-polyfills.js', 'node_modules/systemjs/dist/system.src.js',
1415
'node_modules/whatwg-fetch/fetch.js',

karma-build.conf.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
module.exports = function (config) {
9+
module.exports = function(config) {
1010
require('./karma-base.conf.js')(config);
1111
config.files.push('build/test/wtf_mock.js');
1212
config.files.push('build/test/test_fake_polyfill.js');
1313
config.files.push('build/lib/zone.js');
1414
config.files.push('build/lib/common/promise.js');
15-
config.files.push('build/lib/common/error-rewrite.js');
1615
config.files.push('build/test/main.js');
1716
};

lib/browser/define-property.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const _create = Object.create;
1919
const unconfigurablesKey = zoneSymbol('unconfigurables');
2020

2121
export function propertyPatch() {
22-
Object.defineProperty = function(obj, prop, desc) {
22+
Object.defineProperty = function(obj: any, prop: string, desc: any) {
2323
if (isUnconfigurable(obj, prop)) {
2424
throw new TypeError('Cannot assign to read only property \'' + prop + '\' of ' + obj);
2525
}

0 commit comments

Comments
 (0)