Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/zone.js/lib/common/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function patchPromise(Zone: ZoneType): void {
api.onUnhandledError = (e: any) => {
if (api.showUncaughtError()) {
const rejection = e && e.rejection;
if (rejection) {
if (rejection && e.zone && e.task) {
console.error(
'Unhandled Promise rejection:',
rejection instanceof Error ? rejection.message : rejection,
Expand Down
46 changes: 27 additions & 19 deletions packages/zone.js/lib/zone-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1435,10 +1435,13 @@ export function initZone(): ZoneType {
task.runCount++;
return task.zone.runTask(task, target, args);
} finally {
if (_numberOfNestedTaskFrames === 1 && !global[enableNativeMicrotaskDraining]) {
drainMicroTaskQueueSynchronously();
try {
if (_numberOfNestedTaskFrames === 1 && !global[enableNativeMicrotaskDraining]) {
drainMicroTaskQueueSynchronously();
}
} finally {
_numberOfNestedTaskFrames--;
}
_numberOfNestedTaskFrames--;
}
}

Expand Down Expand Up @@ -1551,27 +1554,32 @@ export function initZone(): ZoneType {

_isDrainingMicrotaskQueue = true;

while (_microTaskQueue.length) {
const queue = _microTaskQueue;
_microTaskQueue = [];
try {
while (_microTaskQueue.length) {
const queue = _microTaskQueue;
_microTaskQueue = [];

for (const task of queue) {
for (const task of queue) {
try {
task.zone.runTask(task, null, null);
} catch (error) {
_api.onUnhandledError(error as Error);
}
}
}
} finally {
// The order matters!
if (global[enableNativeMicrotaskDraining]) {
_isDrainingMicrotaskQueue = false;
_api.microtaskDrainDone();
} else {
try {
task.zone.runTask(task, null, null);
} catch (error) {
_api.onUnhandledError(error as Error);
_api.microtaskDrainDone();
} finally {
_isDrainingMicrotaskQueue = false;
}
}
}

// The order matters!
if (global[enableNativeMicrotaskDraining]) {
_isDrainingMicrotaskQueue = false;
_api.microtaskDrainDone();
} else {
_api.microtaskDrainDone();
_isDrainingMicrotaskQueue = false;
}
}

//////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,28 @@ describe('disable wrap uncaught promise rejection', () => {
done();
});
});

it('should handle a custom object rejection with a rejection property without crashing the error logger', async () => {
await jasmine.spyOnGlobalErrorsAsync(() => {
const originalConsoleError = console.error;
console.error = jasmine.createSpy('consoleErr');

const rejectObj = {
rejection: 'custom-inner-rejection',
message: 'custom-error-message',
};

Zone.current.fork({name: 'promise-error-zone'}).run(() => {
Promise.reject(rejectObj);
});

return new Promise<void>((res) => {
setTimeout(() => {
expect(console.error).toHaveBeenCalledWith(rejectObj);
console.error = originalConsoleError;
res();
});
});
});
});
});
Loading