Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop notifications for errors writing to system log #18022

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading