fix(zone.js): don't fire unhandledrejection if Zone handled error (#31718)

Close #31701

PR Close #31718
This commit is contained in:
JiaLiPassion 2019-07-21 19:49:41 +09:00 committed by Miško Hevery
parent 2172368eae
commit c7542a1d09
2 changed files with 84 additions and 1 deletions

View File

@ -41,10 +41,40 @@ Zone.__load_patch('bluebird', (global: any, Zone: ZoneType, api: _ZonePrivate) =
});
});
if (typeof window !== 'undefined') {
window.addEventListener('unhandledrejection', function(event: any) {
const error = event.detail && event.detail.reason;
if (error && error.isHandledByZone) {
event.preventDefault();
if (typeof event.stopImmediatePropagation === 'function') {
event.stopImmediatePropagation();
}
}
});
} else if (typeof process !== 'undefined') {
process.on('unhandledRejection', (reason: any, p: any) => {
if (reason && reason.isHandledByZone) {
const listeners = process.listeners('unhandledRejection');
if (listeners) {
// remove unhandledRejection listeners so the callback
// will not be triggered.
process.removeAllListeners('unhandledRejection');
process.nextTick(() => {
listeners.forEach(listener => process.on('unhandledRejection', listener));
});
}
}
});
}
Bluebird.onPossiblyUnhandledRejection(function(e: any, promise: any) {
try {
Zone.current.runGuarded(() => { throw e; });
Zone.current.runGuarded(() => {
e.isHandledByZone = true;
throw e;
});
} catch (err) {
err.isHandledByZone = false;
api.onUnhandledError(err);
}
});

View File

@ -700,4 +700,57 @@ describe('bluebird promise', () => {
zone.runGuarded(() => { return Promise.resolve().then(() => { throw new Error('test'); }); });
});
it('should not trigger unhandledrejection if zone.onHandleError return false', (done: DoneFn) => {
const listener = function() { fail('should not be here'); };
if (typeof window !== 'undefined') {
window.addEventListener('unhandledrejection', listener);
} else if (typeof process !== 'undefined') {
process.on('unhandledRejection', listener);
}
const zone = Zone.current.fork({
name: 'testErrorHandling',
onHandleError: function() {
setTimeout(() => {
if (typeof window !== 'undefined') {
window.removeEventListener('unhandledrejection', listener);
} else if (typeof process !== 'undefined') {
process.removeListener('unhandledRejection', listener);
}
done();
}, 500);
return false;
}
});
zone.runGuarded(() => { return Promise.resolve().then(() => { throw new Error('test'); }); });
});
it('should trigger unhandledrejection if zone.onHandleError return true', (done: DoneFn) => {
const listener = function(event: any) {
if (typeof window !== 'undefined') {
expect(event.detail.reason.message).toEqual('test');
} else if (typeof process !== 'undefined') {
expect(event.message).toEqual('test');
}
if (typeof window !== 'undefined') {
window.removeEventListener('unhandledrejection', listener);
} else if (typeof process !== 'undefined') {
process.removeListener('unhandledRejection', listener);
}
done();
};
if (typeof window !== 'undefined') {
window.addEventListener('unhandledrejection', listener);
} else if (typeof process !== 'undefined') {
process.on('unhandledRejection', listener);
}
const zone =
Zone.current.fork({name: 'testErrorHandling', onHandleError: function() { return true; }});
zone.runGuarded(() => { return Promise.resolve().then(() => { throw new Error('test'); }); });
});
});