Handle errrors/wrong values on paste (#25245)

This commit is contained in:
Bram Kragten
2025-04-30 18:45:35 +03:00
committed by GitHub
parent 055c18463c
commit 221bc732fb
4 changed files with 181 additions and 145 deletions

View File

@@ -238,75 +238,90 @@ export class HaManualScriptEditor extends LitElement {
return;
}
const loaded: any = load(paste);
if (loaded) {
let config = loaded;
let loaded: any;
try {
loaded = load(paste);
} catch (_err: any) {
showToast(this, {
message: this.hass.localize(
"ui.panel.config.script.editor.paste_invalid_config"
),
duration: 4000,
dismissable: true,
});
return;
}
if ("script" in config) {
config = config.script;
if (Object.keys(config).length) {
config = config[Object.keys(config)[0]];
}
if (!loaded || typeof loaded !== "object") {
return;
}
let config = loaded;
if ("script" in config) {
config = config.script;
if (Object.keys(config).length) {
config = config[Object.keys(config)[0]];
}
}
if (Array.isArray(config)) {
if (config.length === 1) {
config = config[0];
} else {
config = { sequence: config };
}
if (Array.isArray(config)) {
if (config.length === 1) {
config = config[0];
} else {
config = { sequence: config };
}
}
if (!["sequence", "unknown"].includes(getActionType(config))) {
config = { sequence: [config] };
}
if (!["sequence", "unknown"].includes(getActionType(config))) {
config = { sequence: [config] };
}
let normalized: ScriptConfig | undefined;
let normalized: ScriptConfig | undefined;
try {
normalized = normalizeScriptConfig(config);
} catch (_err: any) {
return;
}
try {
normalized = normalizeScriptConfig(config);
} catch (_err: any) {
return;
}
try {
assert(normalized, scriptConfigStruct);
} catch (_err: any) {
showToast(this, {
message: this.hass.localize(
"ui.panel.config.script.editor.paste_invalid_config"
),
duration: 4000,
dismissable: true,
});
return;
}
try {
assert(normalized, scriptConfigStruct);
} catch (_err: any) {
showToast(this, {
message: this.hass.localize(
"ui.panel.config.script.editor.paste_invalid_config"
),
duration: 4000,
dismissable: true,
});
return;
}
if (normalized) {
ev.preventDefault();
if (normalized) {
ev.preventDefault();
if (this.dirty) {
const result = await new Promise<boolean>((resolve) => {
showPasteReplaceDialog(this, {
domain: "script",
pastedConfig: normalized,
onClose: () => resolve(false),
onAppend: () => {
this._appendToExistingConfig(normalized);
resolve(false);
},
onReplace: () => resolve(true),
});
if (this.dirty) {
const result = await new Promise<boolean>((resolve) => {
showPasteReplaceDialog(this, {
domain: "script",
pastedConfig: normalized,
onClose: () => resolve(false),
onAppend: () => {
this._appendToExistingConfig(normalized);
resolve(false);
},
onReplace: () => resolve(true),
});
});
if (!result) {
return;
}
if (!result) {
return;
}
// replace the config completely
this._replaceExistingConfig(normalized);
}
// replace the config completely
this._replaceExistingConfig(normalized);
}
};