From 96f103644a68bf034614629e97bb9024c10c8330 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Fri, 19 Nov 2021 22:22:49 +0100 Subject: [PATCH] Send error message to sender (#10660) --- cast/src/receiver/layout/hc-main.ts | 42 ++++++++++++++++++++++++++--- src/cast/sender_messages.ts | 16 +++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/cast/src/receiver/layout/hc-main.ts b/cast/src/receiver/layout/hc-main.ts index f74d9089cb..935456a98f 100644 --- a/cast/src/receiver/layout/hc-main.ts +++ b/cast/src/receiver/layout/hc-main.ts @@ -13,7 +13,11 @@ import { ShowDemoMessage, ShowLovelaceViewMessage, } from "../../../../src/cast/receiver_messages"; -import { ReceiverStatusMessage } from "../../../../src/cast/sender_messages"; +import { + ReceiverErrorCode, + ReceiverErrorMessage, + ReceiverStatusMessage, +} from "../../../../src/cast/sender_messages"; import { atLeastVersion } from "../../../../src/common/config/version"; import { isNavigationClick } from "../../../../src/common/dom/is-navigation-click"; import { @@ -134,6 +138,26 @@ export class HcMain extends HassElement { } } + private _sendError( + error_code: number, + error_message: string, + senderId?: string + ) { + const error: ReceiverErrorMessage = { + type: "receiver_error", + error_code, + error_message, + }; + + if (senderId) { + this.sendMessage(senderId, error); + } else { + for (const sender of castContext.getSenders()) { + this.sendMessage(sender.id, error); + } + } + } + private _dialogClosed = () => { document.body.setAttribute("style", "overflow-y: auto !important"); }; @@ -156,14 +180,18 @@ export class HcMain extends HassElement { }), }); } catch (err: any) { - this._error = this._getErrorMessage(err); + const errorMessage = this._getErrorMessage(err); + this._error = errorMessage; + this._sendError(err, errorMessage); return; } let connection; try { connection = await createConnection({ auth }); } catch (err: any) { - this._error = this._getErrorMessage(err); + const errorMessage = this._getErrorMessage(err); + this._error = errorMessage; + this._sendError(err, errorMessage); return; } if (this.hass) { @@ -181,8 +209,10 @@ export class HcMain extends HassElement { if (!this.hass) { this._sendStatus(msg.senderId!); this._error = "Cannot show Lovelace because we're not connected."; + this._sendError(ReceiverErrorCode.NOT_CONNECTED, this._error); return; } + this._error = undefined; if (msg.urlPath === "lovelace") { msg.urlPath = null; } @@ -204,10 +234,14 @@ export class HcMain extends HassElement { this._handleNewLovelaceConfig(lovelaceConfig) ); } catch (err: any) { - if (err.code !== "config_not_found") { + if ( + atLeastVersion(this.hass.connection.haVersion, 0, 107) && + err.code !== "config_not_found" + ) { // eslint-disable-next-line console.log("Error fetching Lovelace configuration", err, msg); this._error = `Error fetching Lovelace configuration: ${err.message}`; + this._sendError(ReceiverErrorCode.FETCH_CONFIG_FAILED, this._error); return; } // Generate a Lovelace config. diff --git a/src/cast/sender_messages.ts b/src/cast/sender_messages.ts index e9a7f074a0..1a6eda58f0 100644 --- a/src/cast/sender_messages.ts +++ b/src/cast/sender_messages.ts @@ -11,4 +11,20 @@ export interface ReceiverStatusMessage extends BaseCastMessage { urlPath?: string | null; } +export interface ReceiverErrorMessage extends BaseCastMessage { + type: "receiver_error"; + error_code: ReceiverErrorCode; + error_message: string; +} + +export const enum ReceiverErrorCode { + CONNECTION_FAILED = 1, + AUTHENTICATION_FAILED = 2, + CONNECTION_LOST = 3, + HASS_URL_MISSING = 4, + NO_HTTPS = 5, + NOT_CONNECTED = 21, + FETCH_CONFIG_FAILED = 22, +} + export type SenderMessage = ReceiverStatusMessage;