mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-11 14:50:02 +00:00
fix(producer): normalize error messages to prevent [object Object] in telemetry (#1099)
* fix(producer): normalize error messages to prevent [object Object] in telemetry When a render fails and the caught value is a plain object (not an Error instance), String(error) produces [object Object], masking the real error in PostHog telemetry (~24 errors/day). Add normalizeErrorMessage() that tries Error.message, string passthrough, .message on plain objects, JSON.stringify, and String() as a last resort. Apply it on the two telemetry-feeding paths: the main render failure handler (renderOrchestrator.ts:2099) and buildRenderErrorDetails (cleanup.ts), plus the error classifier isRecoverableParallelCaptureError so timeout detection works even when the thrown value is a plain object. * fix: address review — normalize CLI telemetry path, captureCost fallback * fix: use local normalizeErrorMessage in CLI to avoid cross-package resolution The Vite test runner can't resolve runtime imports from @hyperframes/producer since its exports point to dist/. Copy the utility into the CLI package and import locally instead.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { normalizeErrorMessage } from "./errorMessage.js";
|
||||
|
||||
describe("normalizeErrorMessage", () => {
|
||||
it("extracts message from Error instances", () => {
|
||||
expect(normalizeErrorMessage(new Error("boom"))).toBe("boom");
|
||||
});
|
||||
|
||||
it("passes through strings", () => {
|
||||
expect(normalizeErrorMessage("oops")).toBe("oops");
|
||||
});
|
||||
|
||||
it("extracts .message from plain objects", () => {
|
||||
expect(normalizeErrorMessage({ message: "hidden error" })).toBe("hidden error");
|
||||
});
|
||||
|
||||
it("JSON-stringifies objects without .message", () => {
|
||||
expect(normalizeErrorMessage({ code: 42 })).toBe('{"code":42}');
|
||||
});
|
||||
|
||||
it("handles null", () => {
|
||||
expect(normalizeErrorMessage(null)).toBe("unknown error");
|
||||
});
|
||||
|
||||
it("handles undefined", () => {
|
||||
expect(normalizeErrorMessage(undefined)).toBe("unknown error");
|
||||
});
|
||||
|
||||
it("handles numbers", () => {
|
||||
expect(normalizeErrorMessage(42)).toBe("42");
|
||||
});
|
||||
|
||||
it("handles objects with non-string .message", () => {
|
||||
expect(normalizeErrorMessage({ message: 123 })).toBe('{"message":123}');
|
||||
});
|
||||
|
||||
it("handles circular references gracefully", () => {
|
||||
const obj: Record<string, unknown> = {};
|
||||
obj.self = obj;
|
||||
// Falls through JSON.stringify failure to Object.keys()
|
||||
expect(normalizeErrorMessage(obj)).toBe("{self}");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Normalize an unknown thrown value into a human-readable string.
|
||||
*
|
||||
* The default `String(error)` pattern produces `[object Object]` when the
|
||||
* thrown value is a plain object — masking the real error in telemetry.
|
||||
* This utility tries, in order:
|
||||
* 1. `Error.message`
|
||||
* 2. Raw string pass-through
|
||||
* 3. `.message` property on a plain object
|
||||
* 4. `JSON.stringify` for any other object
|
||||
* 5. `String()` fallback for primitives (number, boolean, symbol, bigint)
|
||||
* 6. `"unknown error"` for null / undefined
|
||||
*/
|
||||
export function normalizeErrorMessage(error: unknown): string {
|
||||
if (error instanceof Error) return error.message;
|
||||
if (typeof error === "string") return error;
|
||||
if (typeof error === "object" && error !== null) {
|
||||
const msg = (error as Record<string, unknown>).message;
|
||||
if (typeof msg === "string") return msg;
|
||||
try {
|
||||
return JSON.stringify(error);
|
||||
} catch {
|
||||
try {
|
||||
return `{${Object.keys(error as object).join(", ")}}`;
|
||||
} catch {
|
||||
/* truly opaque object */
|
||||
}
|
||||
}
|
||||
}
|
||||
return String(error ?? "unknown error");
|
||||
}
|
||||
Reference in New Issue
Block a user