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
This commit is contained in:
parent
0f8e710c7c
commit
8456c5ec60
|
@ -20,6 +20,8 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
|
||||||
|
|
||||||
const __symbol__ = api.symbol;
|
const __symbol__ = api.symbol;
|
||||||
const _uncaughtPromiseErrors: UncaughtPromiseError[] = [];
|
const _uncaughtPromiseErrors: UncaughtPromiseError[] = [];
|
||||||
|
const isDisableWrappingUncaughtPromiseRejection =
|
||||||
|
global[__symbol__('DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION')] === true;
|
||||||
const symbolPromise = __symbol__('Promise');
|
const symbolPromise = __symbol__('Promise');
|
||||||
const symbolThen = __symbol__('then');
|
const symbolThen = __symbol__('then');
|
||||||
const creationTrace = '__creationTrace__';
|
const creationTrace = '__creationTrace__';
|
||||||
|
@ -41,13 +43,11 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
|
||||||
|
|
||||||
api.microtaskDrainDone = () => {
|
api.microtaskDrainDone = () => {
|
||||||
while (_uncaughtPromiseErrors.length) {
|
while (_uncaughtPromiseErrors.length) {
|
||||||
while (_uncaughtPromiseErrors.length) {
|
const uncaughtPromiseError: UncaughtPromiseError = _uncaughtPromiseErrors.shift() !;
|
||||||
const uncaughtPromiseError: UncaughtPromiseError = _uncaughtPromiseErrors.shift() !;
|
try {
|
||||||
try {
|
uncaughtPromiseError.zone.runGuarded(() => { throw uncaughtPromiseError; });
|
||||||
uncaughtPromiseError.zone.runGuarded(() => { throw uncaughtPromiseError; });
|
} catch (error) {
|
||||||
} catch (error) {
|
handleUnhandledRejection(error);
|
||||||
handleUnhandledRejection(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -58,7 +58,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
|
||||||
api.onUnhandledError(e);
|
api.onUnhandledError(e);
|
||||||
try {
|
try {
|
||||||
const handler = (Zone as any)[UNHANDLED_PROMISE_REJECTION_HANDLER_SYMBOL];
|
const handler = (Zone as any)[UNHANDLED_PROMISE_REJECTION_HANDLER_SYMBOL];
|
||||||
if (handler && typeof handler === 'function') {
|
if (typeof handler === 'function') {
|
||||||
handler.call(this, e);
|
handler.call(this, e);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -176,20 +176,28 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
|
||||||
}
|
}
|
||||||
if (queue.length == 0 && state == REJECTED) {
|
if (queue.length == 0 && state == REJECTED) {
|
||||||
(promise as any)[symbolState] = REJECTED_NO_CATCH;
|
(promise as any)[symbolState] = REJECTED_NO_CATCH;
|
||||||
try {
|
let uncaughtPromiseError = value;
|
||||||
// try to print more readable error log
|
if (!isDisableWrappingUncaughtPromiseRejection) {
|
||||||
throw new Error(
|
// If disable wrapping uncaught promise reject
|
||||||
'Uncaught (in promise): ' + readableObjectToString(value) +
|
// and the rejected value is an Error object,
|
||||||
(value && value.stack ? '\n' + value.stack : ''));
|
// use the value instead of wrapping it.
|
||||||
} catch (err) {
|
try {
|
||||||
const error: UncaughtPromiseError = err;
|
// Here we throws a new Error to print more readable error log
|
||||||
error.rejection = value;
|
// and if the value is not an error, zone.js builds an `Error`
|
||||||
error.promise = promise;
|
// Object here to attach the stack information.
|
||||||
error.zone = Zone.current;
|
throw new Error(
|
||||||
error.task = Zone.currentTask !;
|
'Uncaught (in promise): ' + readableObjectToString(value) +
|
||||||
_uncaughtPromiseErrors.push(error);
|
(value && value.stack ? '\n' + value.stack : ''));
|
||||||
api.scheduleMicroTask(); // to make sure that it is running
|
} catch (err) {
|
||||||
|
uncaughtPromiseError = err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
uncaughtPromiseError.rejection = value;
|
||||||
|
uncaughtPromiseError.promise = promise;
|
||||||
|
uncaughtPromiseError.zone = Zone.current;
|
||||||
|
uncaughtPromiseError.task = Zone.currentTask !;
|
||||||
|
_uncaughtPromiseErrors.push(uncaughtPromiseError);
|
||||||
|
api.scheduleMicroTask(); // to make sure that it is running
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -529,6 +529,17 @@ interface ZoneGlobalConfigurations {
|
||||||
* The preceding code makes all scroll event listeners passive.
|
* The preceding code makes all scroll event listeners passive.
|
||||||
*/
|
*/
|
||||||
__zone_symbol__PASSIVE_EVENTS?: boolean;
|
__zone_symbol__PASSIVE_EVENTS?: boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disable wrapping uncaught promise rejection.
|
||||||
|
*
|
||||||
|
* By default, `zone.js` wraps the uncaught promise rejection in a new `Error` object
|
||||||
|
* which contains additional information such as a value of the rejection and a stack trace.
|
||||||
|
*
|
||||||
|
* If you set `__zone_symbol__DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION = true;` before
|
||||||
|
* importing `zone.js`, `zone.js` will not wrap the uncaught promise rejection.
|
||||||
|
*/
|
||||||
|
__zone_symbol__DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -33,6 +33,7 @@ ts_library(
|
||||||
],
|
],
|
||||||
exclude = [
|
exclude = [
|
||||||
"common/Error.spec.ts",
|
"common/Error.spec.ts",
|
||||||
|
"common/promise-disable-wrap-uncaught-promise-rejection.spec.ts",
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
deps = [
|
deps = [
|
||||||
|
@ -253,7 +254,10 @@ env_entry_point = ":browser-env-setup.ts"
|
||||||
|
|
||||||
test_srcs = glob(
|
test_srcs = glob(
|
||||||
["browser/*.ts"],
|
["browser/*.ts"],
|
||||||
exclude = ["browser/shadydom.spec.ts"],
|
exclude = [
|
||||||
|
"browser/shadydom.spec.ts",
|
||||||
|
"common/promise-disable-wrap-uncaught-promise-rejection.spec.ts",
|
||||||
|
],
|
||||||
) + [
|
) + [
|
||||||
"extra/cordova.spec.ts",
|
"extra/cordova.spec.ts",
|
||||||
"mocha-patch.spec.ts",
|
"mocha-patch.spec.ts",
|
||||||
|
@ -323,3 +327,24 @@ karma_test(
|
||||||
"browser_shadydom_entry_point.ts",
|
"browser_shadydom_entry_point.ts",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
karma_test(
|
||||||
|
name = "browser_disable_wrap_uncaught_promise_rejection",
|
||||||
|
bootstraps = {"browser_disable_wrap_uncaught_promise_rejection": [
|
||||||
|
"//packages/zone.js/dist:zone-testing-bundle.js",
|
||||||
|
]},
|
||||||
|
ci = False,
|
||||||
|
env_deps = [
|
||||||
|
"//packages/zone.js/lib",
|
||||||
|
],
|
||||||
|
env_entry_point = ":browser_disable_wrap_uncaught_promise_rejection_setup.ts",
|
||||||
|
env_srcs = ["browser_disable_wrap_uncaught_promise_rejection_setup.ts"],
|
||||||
|
test_deps = [
|
||||||
|
"//packages/zone.js/lib",
|
||||||
|
],
|
||||||
|
test_entry_point = ":browser_disable_wrap_uncaught_promise_rejection_entry_point.ts",
|
||||||
|
test_srcs = [
|
||||||
|
"common/promise-disable-wrap-uncaught-promise-rejection.spec.ts",
|
||||||
|
"browser_disable_wrap_uncaught_promise_rejection_entry_point.ts",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
/**
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
import './common/promise-disable-wrap-uncaught-promise-rejection.spec';
|
|
@ -0,0 +1,9 @@
|
||||||
|
/**
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
|
||||||
|
(window as any)['__zone_symbol__DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION'] = true;
|
|
@ -0,0 +1,79 @@
|
||||||
|
/**
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
|
||||||
|
class TestRejection {
|
||||||
|
prop1?: string;
|
||||||
|
prop2?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('disable wrap uncaught promise rejection', () => {
|
||||||
|
it('should notify Zone.onHandleError if promise is uncaught', (done) => {
|
||||||
|
let promiseError: Error|null = null;
|
||||||
|
let zone: Zone|null = null;
|
||||||
|
let task: Task|null = null;
|
||||||
|
let error: Error|null = null;
|
||||||
|
Zone.current
|
||||||
|
.fork({
|
||||||
|
name: 'promise-error',
|
||||||
|
onHandleError: (delegate: ZoneDelegate, current: Zone, target: Zone, error: any):
|
||||||
|
boolean => {
|
||||||
|
promiseError = error;
|
||||||
|
delegate.handleError(target, error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.run(() => {
|
||||||
|
zone = Zone.current;
|
||||||
|
task = Zone.currentTask;
|
||||||
|
error = new Error('rejectedErrorShouldBeHandled');
|
||||||
|
try {
|
||||||
|
// throw so that the stack trace is captured
|
||||||
|
throw error;
|
||||||
|
} catch (e) {
|
||||||
|
}
|
||||||
|
Promise.reject(error);
|
||||||
|
expect(promiseError).toBe(null);
|
||||||
|
});
|
||||||
|
setTimeout((): any => null);
|
||||||
|
setTimeout(() => {
|
||||||
|
expect(promiseError).toBe(error);
|
||||||
|
expect((promiseError as any)['rejection']).toBe(error);
|
||||||
|
expect((promiseError as any)['zone']).toBe(zone);
|
||||||
|
expect((promiseError as any)['task']).toBe(task);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should print original information when a non-Error object is used for rejection', (done) => {
|
||||||
|
let promiseError: Error|null = null;
|
||||||
|
let rejectObj: TestRejection;
|
||||||
|
Zone.current
|
||||||
|
.fork({
|
||||||
|
name: 'promise-error',
|
||||||
|
onHandleError: (delegate: ZoneDelegate, current: Zone, target: Zone, error: any):
|
||||||
|
boolean => {
|
||||||
|
promiseError = error;
|
||||||
|
delegate.handleError(target, error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.run(() => {
|
||||||
|
rejectObj = new TestRejection();
|
||||||
|
rejectObj.prop1 = 'value1';
|
||||||
|
rejectObj.prop2 = 'value2';
|
||||||
|
(rejectObj as any).message = 'rejectMessage';
|
||||||
|
Promise.reject(rejectObj);
|
||||||
|
expect(promiseError).toBe(null);
|
||||||
|
});
|
||||||
|
setTimeout((): any => null);
|
||||||
|
setTimeout(() => {
|
||||||
|
expect(promiseError).toEqual(rejectObj as any);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue