Handle errrors/wrong values on paste (#25245)

This commit is contained in:
Bram Kragten 2025-04-30 18:45:35 +03:00 committed by Paul Bottein
parent 46cc254f77
commit efd7b380a9
No known key found for this signature in database
4 changed files with 181 additions and 145 deletions

View File

@ -24,6 +24,10 @@ export class HaToast extends Snackbar {
max-width: 650px; max-width: 650px;
} }
.mdc-snackbar__actions {
color: rgba(255, 255, 255, 0.87);
}
/* Revert the default styles set by mwc-snackbar */ /* Revert the default styles set by mwc-snackbar */
@media (max-width: 480px), (max-width: 344px) { @media (max-width: 480px), (max-width: 344px) {
.mdc-snackbar__surface { .mdc-snackbar__surface {

View File

@ -314,8 +314,24 @@ export class HaManualAutomationEditor extends LitElement {
return; return;
} }
const loaded: any = load(paste); let loaded: any;
if (loaded) { try {
loaded = load(paste);
} catch (_err: any) {
showToast(this, {
message: this.hass.localize(
"ui.panel.config.automation.editor.paste_invalid_yaml"
),
duration: 4000,
dismissable: true,
});
return;
}
if (!loaded || typeof loaded !== "object") {
return;
}
let config = loaded; let config = loaded;
if ("automation" in config) { if ("automation" in config) {
@ -411,7 +427,6 @@ export class HaManualAutomationEditor extends LitElement {
// replace the config completely // replace the config completely
this._replaceExistingConfig(normalized); this._replaceExistingConfig(normalized);
} }
}
}; };
private _appendToExistingConfig(config: ManualAutomationConfig) { private _appendToExistingConfig(config: ManualAutomationConfig) {

View File

@ -238,8 +238,24 @@ export class HaManualScriptEditor extends LitElement {
return; return;
} }
const loaded: any = load(paste); let loaded: any;
if (loaded) { 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 (!loaded || typeof loaded !== "object") {
return;
}
let config = loaded; let config = loaded;
if ("script" in config) { if ("script" in config) {
@ -307,7 +323,6 @@ export class HaManualScriptEditor extends LitElement {
// replace the config completely // replace the config completely
this._replaceExistingConfig(normalized); this._replaceExistingConfig(normalized);
} }
}
}; };
private _appendToExistingConfig(config: ScriptConfig) { private _appendToExistingConfig(config: ScriptConfig) {

View File

@ -4356,6 +4356,7 @@
"text": "How do you want to paste your automation?" "text": "How do you want to paste your automation?"
}, },
"paste_toast_message": "Pasted automation from clipboard", "paste_toast_message": "Pasted automation from clipboard",
"paste_invalid_yaml": "Pasted value is not valid YAML",
"paste_invalid_config": "Pasted automation is not editable in the visual editor" "paste_invalid_config": "Pasted automation is not editable in the visual editor"
}, },
"trace": { "trace": {
@ -4595,6 +4596,7 @@
"text": "How do you want to paste your script?" "text": "How do you want to paste your script?"
}, },
"paste_toast_message": "Pasted script from clipboard", "paste_toast_message": "Pasted script from clipboard",
"paste_invalid_yaml": "Pasted value is not valid YAML",
"paste_invalid_config": "Pasted script is not editable in the visual editor" "paste_invalid_config": "Pasted script is not editable in the visual editor"
}, },
"trace": { "trace": {