🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(trace): capture the real error on I/O parse-worker load failure (LANGFUSE-421/41Z) (#15173)
Nikita Kabardin5e53c7e5c7ff
diff --git a/web/src/hooks/parserWorkerError.clienttest.ts b/web/src/hooks/parserWorkerError.clienttest.tsnew file mode 100644index 0000000000..837672c2c0--- /dev/null+++ b/web/src/hooks/parserWorkerError.clienttest.ts@@ -0,0 +1,98 @@+import { reportParserWorkerError } from "@/src/hooks/parserWorkerError";+import { captureException } from "@sentry/nextjs";++vi.mock("@sentry/nextjs", () => ({+ captureException: vi.fn(),+}));++const mockCaptureException = vi.mocked(captureException);++/**+ * Observability contract for the JSON-parser Web Worker `onerror` path+ * (Sentry LANGFUSE-421 / LANGFUSE-41Z). The raw ErrorEvent used to be+ * `console.error`'d and then stringified by captureConsoleIntegration to the+ * opaque "[object ErrorEvent]", discarding message/filename/lineno.+ * `reportParserWorkerError` must instead capture ONE legible Error with+ * structured context, and log via `console.warn` (never `console.error`) so the+ * same failure is not double-captured.+ */+describe("reportParserWorkerError", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ });++ function makeErrorEvent(overrides: Partial<ErrorEvent> = {}): ErrorEvent {+ return {+ message: "ChunkLoadError: Loading chunk 123 failed",+ filename: "https://app.example/_next/static/chunks/worker.js",+ lineno: 1,+ colno: 42,+ error: undefined,+ ...overrides,+ } as ErrorEvent;+ }++ it("captures a synthesized, legible Error with structured context when event.error is absent", () => {+ const event = makeErrorEvent();+ reportParserWorkerError("useParsedTrace", event);++ expect(mockCaptureException).toHaveBeenCalledTimes(1);+ const [err, opts] = mockCaptureException.mock.calls[0];+ expect(err).toBeInstanceOf(Error);+ expect((err as Error).message).toContain(+ "[useParsedTrace] worker failed to load",+ );+ expect((err as Error).message).toContain(event.message);+ expect((err as Error).message).toContain(event.filename);+ // The fields captureConsoleIntegration used to discard are preserved.+ expect(opts).toMatchObject({+ extra: {+ workerHook: "useParsedTrace",+ message: event.message,+ filename: event.filename,+ lineno: 1,+ colno: 42,+ },+ tags: { area: "io-parse-worker" },+ });+ });++ it("prefers the real Error when the worker threw during init", () => {+ const real = new Error("worker init threw");+ reportParserWorkerError(+ "useParsedObservation",+ makeErrorEvent({ error: real }),+ );++ expect(mockCaptureException).toHaveBeenCalledTimes(1);+ expect(mockCaptureException.mock.calls[0][0]).toBe(real);+ });++ it("logs a readable string via console.warn, not console.error (no double-capture)", () => {+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});+ const error = vi.spyOn(console, "error").mockImplementation(() => {});++ reportParserWorkerError("useParsedTrace", makeErrorEvent());++ // console.error would be re-captured by captureConsoleIntegration.+ expect(error).not.toHaveBeenCalled();+ expect(warn).toHaveBeenCalledTimes(1);+ const logged = String(warn.mock.calls[0][0]);+ expect(logged).toContain("[useParsedTrace] Worker failed to load");+ expect(logged).not.toContain("[object ErrorEvent]");++ warn.mockRestore();+ error.mockRestore();+ });++ it("degrades gracefully when ErrorEvent fields are empty", () => {+ reportParserWorkerError(+ "useParsedTrace",+ makeErrorEvent({ message: "", filename: "" }),+ );++ const [err] = mockCaptureException.mock.calls[0];+ expect((err as Error).message).toContain("unknown");+ expect((err as Error).message).toContain("?");+ });+});diff --git a/web/src/hooks/parserWorkerError.ts b/web/src/hooks/parserWorkerError.tsnew file mode 100644index 0000000000..6e8b3a99af--- /dev/null+++ b/web/src/hooks/parserWorkerError.ts@@ -0,0 +1,50 @@+import { captureException } from "@sentry/nextjs";++/**+ * Turn a JSON-parser Web Worker `onerror` ErrorEvent into a legible,+ * Sentry-reportable error.+ *+ * `Worker.onerror` fires when the worker *script* itself fails to load or throws+ * during init — the dominant cause is a stale-deploy chunk 404. The previous+ * handlers did `console.error("... Worker error:", event)` with the raw+ * ErrorEvent; Sentry's captureConsoleIntegration stringifies that to+ * "[object ErrorEvent]", discarding message/filename/lineno and making the+ * resulting issues undiagnosable.+ *+ * This extracts the real fields, captures one proper Error with structured+ * context, and logs a readable string via `console.warn` (NOT `console.error`,+ * so captureConsoleIntegration — which captures the "error" level — does not+ * double-capture a second, opaque event for the same failure).+ *+ * Note: in-worker parse failures are a separate, already-legible path — they+ * post a "Parse error" string back over the message channel and are handled in+ * the message callback, not here.+ */+export function reportParserWorkerError(+ hookName: string,+ event: ErrorEvent,+): void {+ const details = `${event.message || "unknown"} @ ${event.filename || "?"}:${event.lineno ?? "?"}:${event.colno ?? "?"}`;++ // Prefer the real Error when the worker threw during init; otherwise+ // synthesize one from the ErrorEvent's fields so the message is legible.+ const realError =+ event.error instanceof Error+ ? event.error+ : new Error(`[${hookName}] worker failed to load: ${details}`);++ captureException(realError, {+ extra: {+ workerHook: hookName,+ message: event.message,+ filename: event.filename,+ lineno: event.lineno,+ colno: event.colno,+ },+ tags: { area: "io-parse-worker" },+ });++ // console.warn (not console.error) keeps the console legible without+ // triggering a duplicate capture via captureConsoleIntegration.+ console.warn(`[${hookName}] Worker failed to load: ${details}`);+}diff --git a/web/src/hooks/useParsedObservation.ts b/web/src/hooks/useParsedObservation.tsindex e9d72d9682..92881f6099 100644--- a/web/src/hooks/useParsedObservation.ts+++ b/web/src/hooks/useParsedObservation.ts@@ -26,6 +26,7 @@ import type {ParseResponse,} from "@/src/workers/json-parser.worker";import { cheapHash } from "@/src/hooks/parsedIoCacheKey";+import { reportParserWorkerError } from "@/src/hooks/parserWorkerError";type ObservationWithStringifiedIO = ObservationReturnTypeWithMetadata & {input: string | null;@@ -86,8 +87,8 @@ function getOrCreateWorker(): Worker | null {}};- workerInstance.onerror = (error) => {- console.error("[useParsedObservation] Worker error:", error);+ workerInstance.onerror = (event) => {+ reportParserWorkerError("useParsedObservation", event);};} catch (error) {console.error("[useParsedObservation] Failed to create worker:", error);diff --git a/web/src/hooks/useParsedTrace.ts b/web/src/hooks/useParsedTrace.tsindex 8750c529e4..e5d437ac70 100644--- a/web/src/hooks/useParsedTrace.ts+++ b/web/src/hooks/useParsedTrace.ts@@ -23,6 +23,7 @@ import type {ParseResponse,} from "@/src/workers/json-parser.worker";import { cheapHash } from "@/src/hooks/parsedIoCacheKey";+import { reportParserWorkerError } from "@/src/hooks/parserWorkerError";/*** Threshold for using Web Worker vs sync parsing (in characters).@@ -73,8 +74,8 @@ function getOrCreateWorker(): Worker | null {}};- workerInstance.onerror = (error) => {- console.error("[useParsedTrace] Worker error:", error);+ workerInstance.onerror = (event) => {+ reportParserWorkerError("useParsedTrace", event);};} catch (error) {console.error("[useParsedTrace] Failed to create worker:", error);