commit 9a8f7f22f9bb0ece723f908b4e358992f2d11d08
parent 3880a52dfd6919ecd493a12ccac04e9d0a051428
Author: Mark Striemer <mstriemer@mozilla.com>
Date: Mon, 3 Nov 2025 01:29:38 +0000
Bug 1997451 - Throw when Setting can't be found in setting-control r=tgiles
Differential Revision: https://phabricator.services.mozilla.com/D270781
Diffstat:
2 files changed, 49 insertions(+), 5 deletions(-)
diff --git a/browser/components/preferences/tests/chrome/test_setting_control_checkbox.html b/browser/components/preferences/tests/chrome/test_setting_control_checkbox.html
@@ -408,6 +408,35 @@
ok(control.controlEl.disabled, "Setting is disabled after change");
});
+
+ add_task(async function testSettingNotDefined() {
+ const SETTING = "setting-control-not-defined";
+ let itemConfig = { l10nId: LABEL_L10N_ID, id: SETTING };
+ let setting = Preferences.getSetting(SETTING);
+ try {
+ await renderTemplate(itemConfig, setting);
+ ok(false, "Should throw when setting isn't defined");
+ } catch (e) {
+ let control =
+ testHelpers.renderTarget.querySelector("setting-control");
+ ok(control, "setting-control is rendered");
+ is(control.children.length, 0, "setting-control has no children");
+ let SettingControl = customElements.get("setting-control");
+ is(
+ e.constructor,
+ SettingControl.SettingNotDefinedError,
+ "We got an exception"
+ );
+ }
+
+ Preferences.addSetting({ id: SETTING });
+ let control = await renderTemplate(itemConfig, setting);
+ is(
+ control.firstElementChild.localName,
+ "moz-checkbox",
+ "Rendered with a setting"
+ );
+ });
</script>
</head>
<body>
diff --git a/browser/components/preferences/widgets/setting-control/setting-control.mjs b/browser/components/preferences/widgets/setting-control/setting-control.mjs
@@ -48,12 +48,19 @@ const ITEM_SLOT_BY_PARENT = new Map([
["moz-toggle", "nested"],
]);
-export class SettingControl extends SettingElement {
- /**
- * @type {Setting | undefined}
- */
- #lastSetting;
+export class SettingNotDefinedError extends Error {
+ /** @param {string} settingId */
+ constructor(settingId) {
+ super(
+ `No Setting with id "${settingId}". Did you register it with Preferences.addSetting()?`
+ );
+ this.name = "SettingNotDefinedError";
+ this.settingId = settingId;
+ }
+}
+export class SettingControl extends SettingElement {
+ static SettingNotDefinedError = SettingNotDefinedError;
static properties = {
setting: { type: Object },
config: { type: Object },
@@ -62,6 +69,11 @@ export class SettingControl extends SettingElement {
showEnableExtensionMessage: { type: Boolean },
};
+ /**
+ * @type {Setting | undefined}
+ */
+ #lastSetting;
+
constructor() {
super();
this.controlRef = createRef();
@@ -127,6 +139,9 @@ export class SettingControl extends SettingElement {
this.setValue();
this.setting.on("change", this.onSettingChange);
}
+ if (!this.setting) {
+ throw new SettingNotDefinedError(this.config.id);
+ }
let prevHidden = this.hidden;
this.hidden = !this.setting.visible;
if (prevHidden != this.hidden) {