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 _uncaughtPromiseErrors: UncaughtPromiseError[] = [];
|
||||
const isDisableWrappingUncaughtPromiseRejection =
|
||||
global[__symbol__('DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION')] === true;
|
||||
const symbolPromise = __symbol__('Promise');
|
||||
const symbolThen = __symbol__('then');
|
||||
const creationTrace = '__creationTrace__';
|
||||
|
@ -41,13 +43,11 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
|
|||
|
||||
api.microtaskDrainDone = () => {
|
||||
while (_uncaughtPromiseErrors.length) {
|
||||
while (_uncaughtPromiseErrors.length) {
|
||||
const uncaughtPromiseError: UncaughtPromiseError = _uncaughtPromiseErrors.shift() !;
|
||||
try {
|
||||
uncaughtPromiseError.zone.runGuarded(() => { throw uncaughtPromiseError; });
|
||||
} catch (error) {
|
||||
handleUnhandledRejection(error);
|
||||
}
|
||||
const uncaughtPromiseError: UncaughtPromiseError = _uncaughtPromiseErrors.shift() !;
|
||||
try {
|
||||
uncaughtPromiseError.zone.runGuarded(() => { throw uncaughtPromiseError; });
|
||||
} catch (error) {
|
||||
handleUnhandledRejection(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -58,7 +58,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
|
|||
api.onUnhandledError(e);
|
||||
try {
|
||||
const handler = (Zone as any)[UNHANDLED_PROMISE_REJECTION_HANDLER_SYMBOL];
|
||||
if (handler && typeof handler === 'function') {
|
||||
if (typeof handler === 'function') {
|
||||
handler.call(this, e);
|
||||
}
|
||||
} catch (err) {
|
||||
|
@ -176,20 +176,28 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
|
|||
}
|
||||
if (queue.length == 0 && state == REJECTED) {
|
||||
(promise as any)[symbolState] = REJECTED_NO_CATCH;
|
||||
try {
|
||||
// try to print more readable error log
|
||||
throw new Error(
|
||||
'Uncaught (in promise): ' + readableObjectToString(value) +
|
||||
(value && value.stack ? '\n' + value.stack : ''));
|
||||
} catch (err) {
|
||||
const error: UncaughtPromiseError = err;
|
||||
error.rejection = value;
|
||||
error.promise = promise;
|
||||
error.zone = Zone.current;
|
||||
error.task = Zone.currentTask !;
|
||||
_uncaughtPromiseErrors.push(error);
|
||||
api.scheduleMicroTask(); // to make sure that it is running
|
||||
let uncaughtPromiseError = value;
|
||||
if (!isDisableWrappingUncaughtPromiseRejection) {
|
||||
// If disable wrapping uncaught promise reject
|
||||
// and the rejected value is an Error object,
|
||||
// use the value instead of wrapping it.
|
||||
try {
|
||||
// Here we throws a new Error to print more readable error log
|
||||
// and if the value is not an error, zone.js builds an `Error`
|
||||
// Object here to attach the stack information.
|
||||
throw new Error(
|
||||
'Uncaught (in promise): ' + readableObjectToString(value) +
|
||||
(value && value.stack ? '\n' + value.stack : ''));
|
||||
} 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.
|
||||
*/
|
||||
__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 = [
|
||||
"common/Error.spec.ts",
|
||||
"common/promise-disable-wrap-uncaught-promise-rejection.spec.ts",
|
||||
],
|
||||
),
|
||||
deps = [
|
||||
|
@ -253,7 +254,10 @@ env_entry_point = ":browser-env-setup.ts"
|
|||
|
||||
test_srcs = glob(
|
||||
["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",
|
||||
"mocha-patch.spec.ts",
|
||||
|
@ -323,3 +327,24 @@ karma_test(
|
|||
"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