Skip to content

Commit 45a73dd

Browse files
JiaLiPassionatscott
authored andcommitted
fix(zone.js): defineProperty patch should not swallow error (#37582)
Close #37432 zone.js monkey patches the `Object.defineProperty` API long time ago angular/zone.js@383b479 to resolve issues in very old version of Chrome web which override the property of `CustomElements`, and this is not an issue any longer, so we want to remove this monkey patch, since it may swallow the errors when the user want to define property on unconfigurable or frozen object properties. But currently there are several apps and tests depends on this patch, since it also change the `configurable` property to `true` by default, so in this PR we update the logic to not to swallow error any longer unless the property is the callbacks of `document.registerElements`. BREAKING CHANGE: ZoneJS no longer swallows errors produced by `Object.defineProperty` calls. Prior to this change, ZoneJS monkey patched `Object.defineProperty` and if there is an error (such as the property is not configurable or not writable) the patched logic swallowed it and only console.log was produced. This behavior used to hide real errors, so the logic is now updated to trigger original errors (if any). One exception where the patch remains in place is `document.registerElement` (to allow smooth transition for code/polyfills that rely on old behavior in legacy browsers). If your code relies on the old behavior (where errors were not thrown before), you may need to update the logic to handle the errors that are no longer masked by ZoneJS patch. PR Close #37582
1 parent 6874772 commit 45a73dd

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

packages/zone.js/lib/browser/define-property.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ let _defineProperty: any;
1616
let _getOwnPropertyDescriptor: any;
1717
let _create: any;
1818
let unconfigurablesKey: any;
19+
const registerElementsCallbacks =
20+
['createdCallback', 'attachedCallback', 'detachedCallback', 'attributeChangedCallback'];
1921

2022
export function propertyPatch() {
2123
zoneSymbol = Zone.__symbol__;
@@ -102,6 +104,18 @@ function _tryDefineProperty(obj: any, prop: string, desc: any, originalConfigura
102104
try {
103105
return _defineProperty(obj, prop, desc);
104106
} catch (error) {
107+
let swallowError = false;
108+
if (typeof document !== 'undefined' && obj === document &&
109+
registerElementsCallbacks.find(c => c === prop)) {
110+
// We only swallow the error in registerElement patch
111+
swallowError = true;
112+
}
113+
if (!swallowError) {
114+
throw error;
115+
}
116+
// TODO: @JiaLiPassion, Some application such as `registerElement` patch
117+
// still need to swallow the error, in the future after these applications
118+
// are updated, the following logic can be removed.
105119
let descJson: string|null = null;
106120
try {
107121
descJson = JSON.stringify(desc);

0 commit comments

Comments
 (0)