Show yaml parsing errors in hui-element-editor (#23690)

This commit is contained in:
karwosts 2025-01-13 22:49:09 -08:00 committed by GitHub
parent 9934ea4b4f
commit eda0ea077c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,6 +4,7 @@ import { property, query, state } from "lit/decorators";
import { cache } from "lit/directives/cache";
import type { HASSDomEvent } from "../../../common/dom/fire_event";
import { fireEvent } from "../../../common/dom/fire_event";
import { debounce } from "../../../common/util/debounce";
import { handleStructError } from "../../../common/structs/handle-errors";
import { deepEqual } from "../../../common/util/deep-equal";
import "../../../components/ha-alert";
@ -67,6 +68,11 @@ export abstract class HuiElementEditor<
// Error: Configuration broken - do not save
@state() private _errors?: string[];
// Error from unparseable YAML, but don't show it immediately to prevent showing immediately on every keystroke
@state() private _pendingYamlError?: string;
@state() private _yamlError = false;
// Warning: GUI editor can't handle configuration - ok to save
@state() private _warnings?: string[];
@ -237,6 +243,7 @@ export abstract class HuiElementEditor<
autofocus
.hass=${this.hass}
@value-changed=${this._handleYAMLChanged}
@blur=${this._onBlurYaml}
@keydown=${this._ignoreKeydown}
dir="ltr"
></ha-yaml-editor>
@ -327,6 +334,34 @@ export abstract class HuiElementEditor<
if (ev.detail.isValid) {
this._config = config;
this._errors = undefined;
this._pendingYamlError = undefined;
this._yamlError = false;
this._debounceYamlError.cancel();
this._setConfig();
} else if (this._yamlError) {
// If we're already showing a yaml error, don't bother to debounce, just update immediately.
this._errors = [ev.detail.errorMsg];
} else {
this._pendingYamlError = ev.detail.errorMsg;
this._debounceYamlError();
}
}
private _debounceYamlError = debounce(() => {
if (this._pendingYamlError) {
this._yamlError = true;
this._errors = [this._pendingYamlError];
this._pendingYamlError = undefined;
this._setConfig();
}
}, 2000);
private _onBlurYaml() {
this._debounceYamlError.cancel();
if (this._pendingYamlError) {
this._yamlError = true;
this._errors = [this._pendingYamlError];
this._pendingYamlError = undefined;
this._setConfig();
}
}