Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 4baeb5c

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(showError): fix ignoreConsoleErrorUncaughtError may change during drain microtask (#763)
* fix(showError): fix ignoreConsoleErrorUncaughtError may change during drainMicroTask * refactor(private): change showError from parameter to function
1 parent 97c03b5 commit 4baeb5c

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

lib/common/promise.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
1818
const symbolPromise = __symbol__('Promise');
1919
const symbolThen = __symbol__('then');
2020

21-
api.onUnhandledError = (showError: boolean, e: any) => {
22-
if (showError) {
21+
api.onUnhandledError = (e: any) => {
22+
if (api.showUncaughtError()) {
2323
const rejection = e && e.rejection;
2424
if (rejection) {
2525
console.error(
@@ -32,7 +32,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
3232
}
3333
};
3434

35-
api.microtaskDrainDone = (showError: boolean) => {
35+
api.microtaskDrainDone = () => {
3636
while (_uncaughtPromiseErrors.length) {
3737
while (_uncaughtPromiseErrors.length) {
3838
const uncaughtPromiseError: UncaughtPromiseError = _uncaughtPromiseErrors.shift();
@@ -41,14 +41,14 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
4141
throw uncaughtPromiseError;
4242
});
4343
} catch (error) {
44-
handleUnhandledRejection(showError, error);
44+
handleUnhandledRejection(error);
4545
}
4646
}
4747
}
4848
};
4949

50-
function handleUnhandledRejection(ignoreError: boolean, e: any) {
51-
api.onUnhandledError(ignoreError, e);
50+
function handleUnhandledRejection(e: any) {
51+
api.onUnhandledError(e);
5252
try {
5353
const handler = (Zone as any)[__symbol__('unhandledPromiseRejectionHandler')];
5454
if (handler && typeof handler === 'function') {

lib/zone.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,9 @@ interface _ZonePrivate {
317317
currentZoneFrame(): _ZoneFrame;
318318
symbol(name: string): string;
319319
scheduleMicroTask(task?: MicroTask): void;
320-
onUnhandledError: (showError: boolean, error: Error) => void;
321-
microtaskDrainDone: (showError: boolean) => void;
320+
onUnhandledError: (error: Error) => void;
321+
microtaskDrainDone: () => void;
322+
showUncaughtError: () => boolean;
322323
}
323324

324325
/** @internal */
@@ -1235,7 +1236,6 @@ const Zone: ZoneType = (function(global: any) {
12351236
}
12361237

12371238
function drainMicroTaskQueue() {
1238-
const showError: boolean = !(Zone as any)[__symbol__('ignoreConsoleErrorUncaughtError')];
12391239
if (!_isDrainingMicrotaskQueue) {
12401240
_isDrainingMicrotaskQueue = true;
12411241
while (_microTaskQueue.length) {
@@ -1246,14 +1246,12 @@ const Zone: ZoneType = (function(global: any) {
12461246
try {
12471247
task.zone.runTask(task, null, null);
12481248
} catch (error) {
1249-
if ((Zone as any)[__symbol__('ignoreConsoleErrorUncaughtError')]) {
1250-
return;
1251-
}
1252-
_api.onUnhandledError(showError, error);
1249+
_api.onUnhandledError(error);
12531250
}
12541251
}
12551252
}
1256-
_api.microtaskDrainDone(showError);
1253+
const showError: boolean = !(Zone as any)[__symbol__('ignoreConsoleErrorUncaughtError')];
1254+
_api.microtaskDrainDone();
12571255
_isDrainingMicrotaskQueue = false;
12581256
}
12591257
}
@@ -1278,7 +1276,8 @@ const Zone: ZoneType = (function(global: any) {
12781276
currentZoneFrame: () => _currentZoneFrame,
12791277
onUnhandledError: noop,
12801278
microtaskDrainDone: noop,
1281-
scheduleMicroTask: scheduleMicroTask
1279+
scheduleMicroTask: scheduleMicroTask,
1280+
showUncaughtError: () => !(Zone as any)[__symbol__('ignoreConsoleErrorUncaughtError')]
12821281
};
12831282
let _currentZoneFrame: _ZoneFrame = {parent: null, zone: new Zone(null, null)};
12841283
let _currentTask: Task = null;

test/common/Promise.spec.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,42 @@ describe(
244244
});
245245
});
246246

247-
it('should notify Zone.onError if no one catches promise', (done) => {
247+
it('should output error to console if ignoreConsoleErrorUncaughtError is false',
248+
(done) => {
249+
Zone.current.fork({name: 'promise-error'}).run(() => {
250+
(Zone as any)[Zone.__symbol__('ignoreConsoleErrorUncaughtError')] = false;
251+
const originalConsoleError = console.error;
252+
console.error = jasmine.createSpy('consoleErr');
253+
const p = new Promise((resolve, reject) => {
254+
throw new Error('promise error');
255+
});
256+
setTimeout(() => {
257+
expect(console.error).toHaveBeenCalled();
258+
console.error = originalConsoleError;
259+
done();
260+
}, 10);
261+
});
262+
});
263+
264+
it('should not output error to console if ignoreConsoleErrorUncaughtError is true',
265+
(done) => {
266+
Zone.current.fork({name: 'promise-error'}).run(() => {
267+
(Zone as any)[Zone.__symbol__('ignoreConsoleErrorUncaughtError')] = true;
268+
const originalConsoleError = console.error;
269+
console.error = jasmine.createSpy('consoleErr');
270+
const p = new Promise((resolve, reject) => {
271+
throw new Error('promise error');
272+
});
273+
setTimeout(() => {
274+
expect(console.error).not.toHaveBeenCalled();
275+
console.error = originalConsoleError;
276+
(Zone as any)[Zone.__symbol__('ignoreConsoleErrorUncaughtError')] = false;
277+
done();
278+
}, 10);
279+
});
280+
});
281+
282+
it('should notify Zone.onHandleError if no one catches promise', (done) => {
248283
let promiseError: Error = null;
249284
let zone: Zone = null;
250285
let task: Task = null;

0 commit comments

Comments
 (0)