Display an error if saving new automation times out (#23518)

This commit is contained in:
karwosts 2024-12-31 10:49:45 -08:00 committed by Bram Kragten
parent e8af454705
commit 220011f15f
3 changed files with 52 additions and 3 deletions

View File

@ -1,7 +1,25 @@
class TimeoutError extends Error {
public timeout: number;
constructor(timeout: number, ...params) {
super(...params);
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, TimeoutError);
}
this.name = "TimeoutError";
// Custom debugging information
this.timeout = timeout;
this.message = `Timed out in ${timeout} ms.`;
}
}
export const promiseTimeout = (ms: number, promise: Promise<any> | any) => { export const promiseTimeout = (ms: number, promise: Promise<any> | any) => {
const timeout = new Promise((_resolve, reject) => { const timeout = new Promise((_resolve, reject) => {
setTimeout(() => { setTimeout(() => {
reject(`Timed out in ${ms} ms.`); reject(new TimeoutError(ms));
}, ms); }, ms);
}); });

View File

@ -27,6 +27,7 @@ import { fireEvent } from "../../../common/dom/fire_event";
import { navigate } from "../../../common/navigate"; import { navigate } from "../../../common/navigate";
import { computeRTL } from "../../../common/util/compute_rtl"; import { computeRTL } from "../../../common/util/compute_rtl";
import { afterNextRender } from "../../../common/util/render-status"; import { afterNextRender } from "../../../common/util/render-status";
import { promiseTimeout } from "../../../common/util/promise-timeout";
import "../../../components/ha-button-menu"; import "../../../components/ha-button-menu";
import "../../../components/ha-fab"; import "../../../components/ha-fab";
import "../../../components/ha-icon"; import "../../../components/ha-icon";
@ -944,8 +945,34 @@ export class HaAutomationEditor extends PreventUnsavedMixin(
// wait for automation to appear in entity registry when creating a new automation // wait for automation to appear in entity registry when creating a new automation
if (entityRegPromise) { if (entityRegPromise) {
const automation = await entityRegPromise; try {
entityId = automation.entity_id; const automation = await promiseTimeout(2000, entityRegPromise);
entityId = automation.entity_id;
} catch (e) {
if (e instanceof Error && e.name === "TimeoutError") {
showAlertDialog(this, {
title: this.hass.localize(
"ui.panel.config.automation.editor.new_automation_setup_failed_title",
{
type: this.hass.localize(
"ui.panel.config.automation.editor.type_automation"
),
}
),
text: this.hass.localize(
"ui.panel.config.automation.editor.new_automation_setup_failed_text",
{
type: this.hass.localize(
"ui.panel.config.automation.editor.type_automation"
),
}
),
warning: true,
});
} else {
throw e;
}
}
} }
if (entityId) { if (entityId) {

View File

@ -3065,6 +3065,10 @@
"unknown_entity": "unknown entity", "unknown_entity": "unknown entity",
"edit_unknown_device": "Editor not available for unknown device", "edit_unknown_device": "Editor not available for unknown device",
"switch_ui_yaml_error": "There are currently YAML errors in the automation, and it cannot be parsed. Switching to UI mode may cause pending changes to be lost. Press cancel to correct any errors before proceeding to prevent loss of pending changes, or continue if you are sure.", "switch_ui_yaml_error": "There are currently YAML errors in the automation, and it cannot be parsed. Switching to UI mode may cause pending changes to be lost. Press cancel to correct any errors before proceeding to prevent loss of pending changes, or continue if you are sure.",
"type_automation": "automation",
"type_script": "script",
"new_automation_setup_failed_title": "New {type} setup failed",
"new_automation_setup_failed_text": "Your new {type} has saved, but waiting for it to setup has timed out. This could be due to errors parsing your configuration.yaml, please check the configuration in developer tools. Your {type} will not be visible until this is corrected, and automations are reloaded. Changes to area, category, or labels were not saved and must be reapplied.",
"triggers": { "triggers": {
"name": "Triggers", "name": "Triggers",
"header": "When", "header": "When",