Repository navigation
Description
Which @angular/* package(s) are the source of the bug?
zone.js
Is this a regression?
No
Description
Setting DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION to true will cause unhandled rejections events to not be emitted because of an exception inside the code that sends the events.
__zone_symbol__DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION = true;
// load zone.js
onunhandledrejection = console.dir;
Promise.reject(new Error("forced error"));The above code doesn't call the unhandled rejection handler. Zone.js triggers an internal exception while trying to emit the event and this event is caught and silently ignored.
thrown from:
| const evt = new PromiseRejectionEvent(evtName, {promise: e.promise, reason: e.rejection}); |
Caught and ignore at:
| } catch (err) { |
Exception thrown
TypeError: Failed to construct 'PromiseRejectionEvent': Failed to read the 'promise' property from 'PromiseRejectionEventInit': Required member is undefined.
The issue is that with DISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTION enabled causes the error send to the handler that emits events to be the original error and not the wrapper error. Only the wrapper error has promise and rejection properties that the handler was using to construct the rejection event.
angular/packages/zone.js/lib/common/promise.ts
Lines 52 to 69 in f7cfc3b
| throw uncaughtPromiseError.rejection; | |
| } | |
| throw uncaughtPromiseError; | |
| }); | |
| } catch (error) { | |
| handleUnhandledRejection(error); | |
| } | |
| } | |
| }; | |
| const UNHANDLED_PROMISE_REJECTION_HANDLER_SYMBOL = __symbol__('unhandledPromiseRejectionHandler'); | |
| function handleUnhandledRejection(this: unknown, e: any) { | |
| api.onUnhandledError(e); | |
| try { | |
| const handler = (Zone as any)[UNHANDLED_PROMISE_REJECTION_HANDLER_SYMBOL]; | |
| if (typeof handler === 'function') { | |
| handler.call(this, e); |
The issue doesn't happen for the promise rejection handled handler since it gets both the promise and the rejection.
| handler.call(this, {rejection: (promise as any)[symbolValue], promise: promise}); |
Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
No response
Please provide the environment you discovered this bug in (run ng version)
No response
Anything else?
No response
Activity
matthewjh commentedon Dec 19, 2024
Unfortunately, since #52492 zone.js breaks
unhandledrejectionhandling even with the default config because the default ofDISABLE_WRAPPING_UNCAUGHT_PROMISE_REJECTIONwas flipped.In every instance I looked at, the exception from the
PromiseRejectionEventctor is swallowed but it is there, with the result that user-addedunhandledrejectionhandlers are just not called at all. Silent errors are not good.@JiaLiPassion