mirror of
https://github.com/home-assistant/frontend.git
synced 2025-11-07 01:50:31 +00:00
Handle errrors/wrong values on paste (#25245)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user