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);
}
});