From 1df1ce5423fbdf56487fb3cb8f9327b805823674 Mon Sep 17 00:00:00 2001 From: Steve Repsher Date: Tue, 26 Sep 2023 08:43:27 -0400 Subject: [PATCH] Stop notifications for errors writing to system log (#18022) --- src/state/connection-mixin.ts | 44 +++++++++++++++++++--------------- src/state/logging-mixin.ts | 45 +++++++++++++++++++---------------- src/types.ts | 3 ++- 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/src/state/connection-mixin.ts b/src/state/connection-mixin.ts index 76e4c5008cef..bfcbabfd7095 100644 --- a/src/state/connection-mixin.ts +++ b/src/state/connection-mixin.ts @@ -77,8 +77,13 @@ export const connectionMixin = >( enableShortcuts: true, moreInfoEntityId: null, hassUrl: (path = "") => new URL(path, auth.data.hassUrl).toString(), - // eslint-disable-next-line @typescript-eslint/default-param-last - callService: async (domain, service, serviceData = {}, target) => { + callService: async ( + domain, + service, + serviceData, + target, + notifyOnError = true + ) => { if (__DEV__) { // eslint-disable-next-line no-console console.log( @@ -94,7 +99,7 @@ export const connectionMixin = >( conn, domain, service, - serviceData, + serviceData ?? {}, target )) as ServiceCallResponse; } catch (err: any) { @@ -111,24 +116,25 @@ export const connectionMixin = >( domain, service, serviceData, - target, - err + target ); } - forwardHaptic("failure"); - const message = - (this as any).hass.localize( - "ui.notification_toast.service_call_failed", - "service", - `${domain}/${service}` - ) + - ` ${ - err.message || - (err.error?.code === ERR_CONNECTION_LOST - ? "connection lost" - : "unknown error") - }`; - fireEvent(this as any, "hass-notification", { message }); + if (notifyOnError) { + forwardHaptic("failure"); + const message = + (this as any).hass.localize( + "ui.notification_toast.service_call_failed", + "service", + `${domain}/${service}` + ) + + ` ${ + err.message || + (err.error?.code === ERR_CONNECTION_LOST + ? "connection lost" + : "unknown error") + }`; + fireEvent(this as any, "hass-notification", { message }); + } throw err; } }, diff --git a/src/state/logging-mixin.ts b/src/state/logging-mixin.ts index d134e651846e..711514f857a4 100644 --- a/src/state/logging-mixin.ts +++ b/src/state/logging-mixin.ts @@ -40,34 +40,30 @@ export const loggingMixin = >( ev.stopPropagation(); return; } - let message; try { const { createLogMessage } = await import("../resources/log-message"); - message = await createLogMessage( + const message = await createLogMessage( ev.error, "Uncaught error", + // The error object from browsers includes the message and a stack trace, + // so use the data in the error event just as fallback ev.message, `@${ev.filename}:${ev.lineno}:${ev.colno}` ); - await this._writeLog({ - // The error object from browsers includes the message and a stack trace, - // so use the data in the error event just as fallback - message, - }); + await this._writeLog({ message }); } catch (e) { - // eslint-disable-next-line no-console - console.error("Error during logging error:", message, e); // catch errors during logging so we don't get into a loop + // eslint-disable-next-line no-console + console.error("Failure writing uncaught error to system log:", e); } }); window.addEventListener("unhandledrejection", async (ev) => { if (!this.hass?.connected) { return; } - let message; try { const { createLogMessage } = await import("../resources/log-message"); - message = await createLogMessage( + const message = await createLogMessage( ev.reason, "Unhandled promise rejection" ); @@ -76,9 +72,12 @@ export const loggingMixin = >( level: "debug", }); } catch (e) { - // eslint-disable-next-line no-console - console.error("Error during logging error:", message, e); // catch errors during logging so we don't get into a loop + // eslint-disable-next-line no-console + console.error( + "Failure writing unhandled promise rejection to system log:", + e + ); } }); } @@ -91,12 +90,18 @@ export const loggingMixin = >( } private _writeLog(log: WriteLogParams) { - return this.hass?.callService("system_log", "write", { - logger: `frontend.${ - __DEV__ ? "js_dev" : "js" - }.${__BUILD__}.${__VERSION__.replace(".", "")}`, - message: log.message, - level: log.level || "error", - }); + return this.hass?.callService( + "system_log", + "write", + { + logger: `frontend.${ + __DEV__ ? "js_dev" : "js" + }.${__BUILD__}.${__VERSION__.replace(".", "")}`, + message: log.message, + level: log.level || "error", + }, + undefined, + false + ); } }; diff --git a/src/types.ts b/src/types.ts index 09eb6c6e584c..ced909e0ef01 100644 --- a/src/types.ts +++ b/src/types.ts @@ -240,7 +240,8 @@ export interface HomeAssistant { domain: ServiceCallRequest["domain"], service: ServiceCallRequest["service"], serviceData?: ServiceCallRequest["serviceData"], - target?: ServiceCallRequest["target"] + target?: ServiceCallRequest["target"], + notifyOnError?: boolean ): Promise; callApi( method: "GET" | "POST" | "PUT" | "DELETE",