1
0
mirror of https://github.com/home-assistant/frontend.git synced 2026-07-04 13:05:06 +01:00
Files
frontend/test/resources/log-message.test.ts
Franck Nijhof 854c57c0e0 Prevent error logger from crashing on unparseable stack traces (#52575)
When createLogMessage received an error whose stack stacktrace-js could
not parse (for example a DOMException such as "AbortError: Transition was
skipped"), fromError threw "Cannot parse given Error object". That throw
escaped createLogMessage, so the global unhandledrejection handler logged
"Failure writing unhandled promise rejection to system log" and the
original error was never recorded.

Wrap the stacktrace extraction in a try/catch and fall back to the raw
error stack (or the provided stack fallback) so the logger stays robust
and still records the original error.
2026-06-14 09:01:31 +03:00

52 lines
1.5 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { createLogMessage } from "../../src/resources/log-message";
const fromError = vi.hoisted(() => vi.fn());
vi.mock("stacktrace-js", () => ({ fromError }));
describe("createLogMessage", () => {
beforeEach(() => {
fromError.mockReset();
});
it("includes the error message and parsed stack frames", async () => {
fromError.mockResolvedValue([
{ fileName: "https://example.com/foo.js", toString: () => "at foo.js" },
]);
const error = new Error("boom");
const message = await createLogMessage(error);
expect(message).toContain("Error: boom");
expect(message).toContain("at foo.js");
});
it("does not throw when stacktrace-js cannot parse the stack", async () => {
fromError.mockRejectedValue(new Error("Cannot parse given Error object"));
const error = new Error("boom");
error.stack = "Error: boom\n at <anonymous>";
const message = await createLogMessage(error);
expect(message).toContain("Error: boom");
// Falls back to the raw stack instead of crashing the logger.
expect(message).toContain("at <anonymous>");
});
it("falls back to the provided stack fallback when no stack is available", async () => {
fromError.mockRejectedValue(new Error("Cannot parse given Error object"));
const error = new Error("boom");
error.stack = undefined;
const message = await createLogMessage(
error,
undefined,
undefined,
"@unknown:0:0"
);
expect(message).toContain("@unknown:0:0");
});
});