Skip to content

Commit 8456c5e

Browse files
JiaLiPassionAndrewKushnir
authored andcommitted
feat(zone.js): add a zone config to allow user disable wrapping uncaught promise rejection (#35873)
Close #27840. By default, `zone.js` wrap uncaught promise error and wrap it to a new Error object with some additional information includes the value of the error and the stack trace. Consider the following example: ``` Zone.current .fork({ name: 'promise-error', onHandleError: (delegate: ZoneDelegate, current: Zone, target: Zone, error: any): boolean => { console.log('caught an error', error); delegate.handleError(target, error); return false; } }).run(() => { const originalError = new Error('testError'); Promise.reject(originalError); }); ``` The `promise-error` zone catches a wrapped `Error` object whose `rejection` property equals to the original error, and the message will be `Uncaught (in promise): testError....`, You can disable this wrapping behavior by defining a global configuraiton `__zone_symbol__DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION = true;` before importing `zone.js`. PR Close #35873
1 parent 0f8e710 commit 8456c5e

6 files changed

Lines changed: 162 additions & 22 deletions

packages/zone.js/lib/common/promise.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
2020

2121
const __symbol__ = api.symbol;
2222
const _uncaughtPromiseErrors: UncaughtPromiseError[] = [];
23+
const isDisableWrappingUncaughtPromiseRejection =
24+
global[__symbol__('DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION')] === true;
2325
const symbolPromise = __symbol__('Promise');
2426
const symbolThen = __symbol__('then');
2527
const creationTrace = '__creationTrace__';
@@ -41,13 +43,11 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
4143

4244
api.microtaskDrainDone = () => {
4345
while (_uncaughtPromiseErrors.length) {
44-
while (_uncaughtPromiseErrors.length) {
45-
const uncaughtPromiseError: UncaughtPromiseError = _uncaughtPromiseErrors.shift() !;
46-
try {
47-
uncaughtPromiseError.zone.runGuarded(() => { throw uncaughtPromiseError; });
48-
} catch (error) {
49-
handleUnhandledRejection(error);
50-
}
46+
const uncaughtPromiseError: UncaughtPromiseError = _uncaughtPromiseErrors.shift() !;
47+
try {
48+
uncaughtPromiseError.zone.runGuarded(() => { throw uncaughtPromiseError; });
49+
} catch (error) {
50+
handleUnhandledRejection(error);
5151
}
5252
}
5353
};
@@ -58,7 +58,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
5858
api.onUnhandledError(e);
5959
try {
6060
const handler = (Zone as any)[UNHANDLED_PROMISE_REJECTION_HANDLER_SYMBOL];
61-
if (handler && typeof handler === 'function') {
61+
if (typeof handler === 'function') {
6262
handler.call(this, e);
6363
}
6464
} catch (err) {
@@ -176,20 +176,28 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
176176
}
177177
if (queue.length == 0 && state == REJECTED) {
178178
(promise as any)[symbolState] = REJECTED_NO_CATCH;
179-
try {
180-
// try to print more readable error log
181-
throw new Error(
182-
'Uncaught (in promise): ' + readableObjectToString(value) +
183-
(value && value.stack ? '\n' + value.stack : ''));
184-
} catch (err) {
185-
const error: UncaughtPromiseError = err;
186-
error.rejection = value;
187-
error.promise = promise;
188-
error.zone = Zone.current;
189-
error.task = Zone.currentTask !;
190-
_uncaughtPromiseErrors.push(error);
191-
api.scheduleMicroTask(); // to make sure that it is running
179+
let uncaughtPromiseError = value;
180+
if (!isDisableWrappingUncaughtPromiseRejection) {
181+
// If disable wrapping uncaught promise reject
182+
// and the rejected value is an Error object,
183+
// use the value instead of wrapping it.
184+
try {
185+
// Here we throws a new Error to print more readable error log
186+
// and if the value is not an error, zone.js builds an `Error`
187+
// Object here to attach the stack information.
188+
throw new Error(
189+
'Uncaught (in promise): ' + readableObjectToString(value) +
190+
(value && value.stack ? '\n' + value.stack : ''));
191+
} catch (err) {
192+
uncaughtPromiseError = err;
193+
}
192194
}
195+
uncaughtPromiseError.rejection = value;
196+
uncaughtPromiseError.promise = promise;
197+
uncaughtPromiseError.zone = Zone.current;
198+
uncaughtPromiseError.task = Zone.currentTask !;
199+
_uncaughtPromiseErrors.push(uncaughtPromiseError);
200+
api.scheduleMicroTask(); // to make sure that it is running
193201
}
194202
}
195203
}

packages/zone.js/lib/zone.configurations.api.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,17 @@ interface ZoneGlobalConfigurations {
529529
* The preceding code makes all scroll event listeners passive.
530530
*/
531531
__zone_symbol__PASSIVE_EVENTS?: boolean;
532+
533+
/**
534+
* Disable wrapping uncaught promise rejection.
535+
*
536+
* By default, `zone.js` wraps the uncaught promise rejection in a new `Error` object
537+
* which contains additional information such as a value of the rejection and a stack trace.
538+
*
539+
* If you set `__zone_symbol__DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION = true;` before
540+
* importing `zone.js`, `zone.js` will not wrap the uncaught promise rejection.
541+
*/
542+
__zone_symbol__DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION?: boolean;
532543
}
533544

534545
/**

packages/zone.js/test/BUILD.bazel

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ ts_library(
3333
],
3434
exclude = [
3535
"common/Error.spec.ts",
36+
"common/promise-disable-wrap-uncaught-promise-rejection.spec.ts",
3637
],
3738
),
3839
deps = [
@@ -253,7 +254,10 @@ env_entry_point = ":browser-env-setup.ts"
253254

254255
test_srcs = glob(
255256
["browser/*.ts"],
256-
exclude = ["browser/shadydom.spec.ts"],
257+
exclude = [
258+
"browser/shadydom.spec.ts",
259+
"common/promise-disable-wrap-uncaught-promise-rejection.spec.ts",
260+
],
257261
) + [
258262
"extra/cordova.spec.ts",
259263
"mocha-patch.spec.ts",
@@ -323,3 +327,24 @@ karma_test(
323327
"browser_shadydom_entry_point.ts",
324328
],
325329
)
330+
331+
karma_test(
332+
name = "browser_disable_wrap_uncaught_promise_rejection",
333+
bootstraps = {"browser_disable_wrap_uncaught_promise_rejection": [
334+
"//packages/zone.js/dist:zone-testing-bundle.js",
335+
]},
336+
ci = False,
337+
env_deps = [
338+
"//packages/zone.js/lib",
339+
],
340+
env_entry_point = ":browser_disable_wrap_uncaught_promise_rejection_setup.ts",
341+
env_srcs = ["browser_disable_wrap_uncaught_promise_rejection_setup.ts"],
342+
test_deps = [
343+
"//packages/zone.js/lib",
344+
],
345+
test_entry_point = ":browser_disable_wrap_uncaught_promise_rejection_entry_point.ts",
346+
test_srcs = [
347+
"common/promise-disable-wrap-uncaught-promise-rejection.spec.ts",
348+
"browser_disable_wrap_uncaught_promise_rejection_entry_point.ts",
349+
],
350+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
import './common/promise-disable-wrap-uncaught-promise-rejection.spec';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
(window as any)['__zone_symbol__DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION'] = true;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
class TestRejection {
10+
prop1?: string;
11+
prop2?: string;
12+
}
13+
14+
describe('disable wrap uncaught promise rejection', () => {
15+
it('should notify Zone.onHandleError if promise is uncaught', (done) => {
16+
let promiseError: Error|null = null;
17+
let zone: Zone|null = null;
18+
let task: Task|null = null;
19+
let error: Error|null = null;
20+
Zone.current
21+
.fork({
22+
name: 'promise-error',
23+
onHandleError: (delegate: ZoneDelegate, current: Zone, target: Zone, error: any):
24+
boolean => {
25+
promiseError = error;
26+
delegate.handleError(target, error);
27+
return false;
28+
}
29+
})
30+
.run(() => {
31+
zone = Zone.current;
32+
task = Zone.currentTask;
33+
error = new Error('rejectedErrorShouldBeHandled');
34+
try {
35+
// throw so that the stack trace is captured
36+
throw error;
37+
} catch (e) {
38+
}
39+
Promise.reject(error);
40+
expect(promiseError).toBe(null);
41+
});
42+
setTimeout((): any => null);
43+
setTimeout(() => {
44+
expect(promiseError).toBe(error);
45+
expect((promiseError as any)['rejection']).toBe(error);
46+
expect((promiseError as any)['zone']).toBe(zone);
47+
expect((promiseError as any)['task']).toBe(task);
48+
done();
49+
});
50+
});
51+
52+
it('should print original information when a non-Error object is used for rejection', (done) => {
53+
let promiseError: Error|null = null;
54+
let rejectObj: TestRejection;
55+
Zone.current
56+
.fork({
57+
name: 'promise-error',
58+
onHandleError: (delegate: ZoneDelegate, current: Zone, target: Zone, error: any):
59+
boolean => {
60+
promiseError = error;
61+
delegate.handleError(target, error);
62+
return false;
63+
}
64+
})
65+
.run(() => {
66+
rejectObj = new TestRejection();
67+
rejectObj.prop1 = 'value1';
68+
rejectObj.prop2 = 'value2';
69+
(rejectObj as any).message = 'rejectMessage';
70+
Promise.reject(rejectObj);
71+
expect(promiseError).toBe(null);
72+
});
73+
setTimeout((): any => null);
74+
setTimeout(() => {
75+
expect(promiseError).toEqual(rejectObj as any);
76+
done();
77+
});
78+
});
79+
});

0 commit comments

Comments
 (0)