Stop notifications for errors writing to system log (#18022)

This commit is contained in:
Steve Repsher 2023-09-26 08:43:27 -04:00 committed by GitHub
parent a68381a4d9
commit 1df1ce5423
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 40 deletions

View File

@ -77,8 +77,13 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
enableShortcuts: true, enableShortcuts: true,
moreInfoEntityId: null, moreInfoEntityId: null,
hassUrl: (path = "") => new URL(path, auth.data.hassUrl).toString(), hassUrl: (path = "") => new URL(path, auth.data.hassUrl).toString(),
// eslint-disable-next-line @typescript-eslint/default-param-last callService: async (
callService: async (domain, service, serviceData = {}, target) => { domain,
service,
serviceData,
target,
notifyOnError = true
) => {
if (__DEV__) { if (__DEV__) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log( console.log(
@ -94,7 +99,7 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
conn, conn,
domain, domain,
service, service,
serviceData, serviceData ?? {},
target target
)) as ServiceCallResponse; )) as ServiceCallResponse;
} catch (err: any) { } catch (err: any) {
@ -111,24 +116,25 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
domain, domain,
service, service,
serviceData, serviceData,
target, target
err
); );
} }
forwardHaptic("failure"); if (notifyOnError) {
const message = forwardHaptic("failure");
(this as any).hass.localize( const message =
"ui.notification_toast.service_call_failed", (this as any).hass.localize(
"service", "ui.notification_toast.service_call_failed",
`${domain}/${service}` "service",
) + `${domain}/${service}`
` ${ ) +
err.message || ` ${
(err.error?.code === ERR_CONNECTION_LOST err.message ||
? "connection lost" (err.error?.code === ERR_CONNECTION_LOST
: "unknown error") ? "connection lost"
}`; : "unknown error")
fireEvent(this as any, "hass-notification", { message }); }`;
fireEvent(this as any, "hass-notification", { message });
}
throw err; throw err;
} }
}, },

View File

@ -40,34 +40,30 @@ export const loggingMixin = <T extends Constructor<HassBaseEl>>(
ev.stopPropagation(); ev.stopPropagation();
return; return;
} }
let message;
try { try {
const { createLogMessage } = await import("../resources/log-message"); const { createLogMessage } = await import("../resources/log-message");
message = await createLogMessage( const message = await createLogMessage(
ev.error, ev.error,
"Uncaught 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.message,
`@${ev.filename}:${ev.lineno}:${ev.colno}` `@${ev.filename}:${ev.lineno}:${ev.colno}`
); );
await this._writeLog({ await this._writeLog({ message });
// The error object from browsers includes the message and a stack trace,
// so use the data in the error event just as fallback
message,
});
} catch (e) { } 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 // 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) => { window.addEventListener("unhandledrejection", async (ev) => {
if (!this.hass?.connected) { if (!this.hass?.connected) {
return; return;
} }
let message;
try { try {
const { createLogMessage } = await import("../resources/log-message"); const { createLogMessage } = await import("../resources/log-message");
message = await createLogMessage( const message = await createLogMessage(
ev.reason, ev.reason,
"Unhandled promise rejection" "Unhandled promise rejection"
); );
@ -76,9 +72,12 @@ export const loggingMixin = <T extends Constructor<HassBaseEl>>(
level: "debug", level: "debug",
}); });
} catch (e) { } 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 // 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 = <T extends Constructor<HassBaseEl>>(
} }
private _writeLog(log: WriteLogParams) { private _writeLog(log: WriteLogParams) {
return this.hass?.callService("system_log", "write", { return this.hass?.callService(
logger: `frontend.${ "system_log",
__DEV__ ? "js_dev" : "js" "write",
}.${__BUILD__}.${__VERSION__.replace(".", "")}`, {
message: log.message, logger: `frontend.${
level: log.level || "error", __DEV__ ? "js_dev" : "js"
}); }.${__BUILD__}.${__VERSION__.replace(".", "")}`,
message: log.message,
level: log.level || "error",
},
undefined,
false
);
} }
}; };

View File

@ -240,7 +240,8 @@ export interface HomeAssistant {
domain: ServiceCallRequest["domain"], domain: ServiceCallRequest["domain"],
service: ServiceCallRequest["service"], service: ServiceCallRequest["service"],
serviceData?: ServiceCallRequest["serviceData"], serviceData?: ServiceCallRequest["serviceData"],
target?: ServiceCallRequest["target"] target?: ServiceCallRequest["target"],
notifyOnError?: boolean
): Promise<ServiceCallResponse>; ): Promise<ServiceCallResponse>;
callApi<T>( callApi<T>(
method: "GET" | "POST" | "PUT" | "DELETE", method: "GET" | "POST" | "PUT" | "DELETE",