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:

committed by
Andrew Kushnir

parent
0f8e710c7c
commit
8456c5ec60
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user