Skip to content

Commit

Permalink
Stop notifications for errors writing to system log (#18022)
Browse files Browse the repository at this point in the history
  • Loading branch information
steverep authored Sep 26, 2023
1 parent a68381a commit 1df1ce5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 40 deletions.
44 changes: 25 additions & 19 deletions src/state/connection-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
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(
Expand All @@ -94,7 +99,7 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
conn,
domain,
service,
serviceData,
serviceData ?? {},
target
)) as ServiceCallResponse;
} catch (err: any) {
Expand All @@ -111,24 +116,25 @@ export const connectionMixin = <T extends Constructor<HassBaseEl>>(
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;
}
},
Expand Down
45 changes: 25 additions & 20 deletions src/state/logging-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,30 @@ export const loggingMixin = <T extends Constructor<HassBaseEl>>(
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"
);
Expand All @@ -76,9 +72,12 @@ export const loggingMixin = <T extends Constructor<HassBaseEl>>(
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
);
}
});
}
Expand All @@ -91,12 +90,18 @@ export const loggingMixin = <T extends Constructor<HassBaseEl>>(
}

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
);
}
};
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServiceCallResponse>;
callApi<T>(
method: "GET" | "POST" | "PUT" | "DELETE",
Expand Down

0 comments on commit 1df1ce5

Please sign in to comment.