🪢 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(web): classify tRPC response JSON-parse failures as transport noise (#15772)
Nikita Kabardinedc3c14d543d
diff --git a/web/src/utils/api.clienttest.ts b/web/src/utils/api.clienttest.tsindex ceba75b3be..c89b311ae8 100644--- a/web/src/utils/api.clienttest.ts+++ b/web/src/utils/api.clienttest.ts@@ -1,12 +1,25 @@import { TRPCClientError } from "@trpc/client";+import { vi } from "vitest";import {EXPECTED_TRPC_ERROR_CODES,+ fetchWithParseErrorStatus,getTrpcErrorCode,getTrpcErrorPath,isExpectedTrpcClientError,isNetworkConnectivityError,+ isTrpcResponseParseError,} from "@/src/utils/api";+/** A JSON.parse SyntaxError annotated with the HTTP status it was parsed+ * from, as produced by `fetchWithParseErrorStatus`. */+const parseErrorWithStatus = (message: string, status: number) => {+ const cause: SyntaxError & { responseStatus?: number } = new SyntaxError(+ message,+ );+ cause.responseStatus = status;+ return cause;+};+/** Builds a TRPCClientError with the given server error shape (code/path). */const trpcServerError = (opts: {code: string;@@ -80,6 +93,127 @@ describe("isNetworkConnectivityError", () => {});});+describe("isTrpcResponseParseError", () => {+ it("detects a JSON.parse failure on the response body (Firefox message)", () => {+ const error = TRPCClientError.from(+ new SyntaxError(+ "JSON.parse: unexpected character at line 1 column 1 of the JSON data",+ ),+ {+ meta: { response: new Response("<html></html>", { status: 200 }) },+ },+ );++ expect(isTrpcResponseParseError(error)).toBe(true);+ });++ it("detects a truncated response body (Chromium message)", () => {+ const error = TRPCClientError.from(+ new SyntaxError("Unexpected end of JSON input"),+ );++ expect(isTrpcResponseParseError(error)).toBe(true);+ });++ // Negative fixtures: real errors MUST still flow to Sentry. If any of these+ // start returning true, the suppression rule has grown a hole that hides a+ // genuine bug.+ it("does not match tRPC server errors (a parsed error envelope)", () => {+ const error = trpcServerError({+ code: "INTERNAL_SERVER_ERROR",+ httpStatus: 500,+ path: "events.all",+ });++ expect(isTrpcResponseParseError(error)).toBe(false);+ });++ it("does not match network connectivity failures", () => {+ const error = TRPCClientError.from(new TypeError("Failed to fetch"));++ expect(isTrpcResponseParseError(error)).toBe(false);+ });++ it("does not match a TRPCClientError with a non-SyntaxError cause", () => {+ const error = TRPCClientError.from(new Error("boom"));++ expect(isTrpcResponseParseError(error)).toBe(false);+ });++ it("does not match a raw JSON.parse SyntaxError thrown by app code", () => {+ // Not wrapped by the tRPC client — in handleTrpcError it takes the+ // non-TRPC branch and is captured unchanged.+ expect(+ isTrpcResponseParseError(new SyntaxError("Unexpected end of JSON input")),+ ).toBe(false);+ expect(isTrpcResponseParseError(null)).toBe(false);+ expect(isTrpcResponseParseError(undefined)).toBe(false);+ });++ // Request-too-large (414/431) parse failures are the app-owned+ // oversized-GET-URL bug class (see sendAsPostOption) and MUST keep+ // flowing to Sentry.+ it.each([414, 431])(+ "does not match a parse failure annotated with HTTP %i",+ (status) => {+ const error = TRPCClientError.from(+ parseErrorWithStatus("Unexpected end of JSON input", status),+ );++ expect(isTrpcResponseParseError(error)).toBe(false);+ },+ );++ it("does not match a parse failure whose link meta shows HTTP 431", () => {+ const error = TRPCClientError.from(+ new SyntaxError("Unexpected end of JSON input"),+ { meta: { response: new Response("", { status: 431 }) } },+ );++ expect(isTrpcResponseParseError(error)).toBe(false);+ });++ it("matches a parse failure annotated with a non-request-too-large status", () => {+ const error = TRPCClientError.from(+ parseErrorWithStatus(+ "JSON.parse: unexpected character at line 1 column 1 of the JSON data",+ 200,+ ),+ );++ expect(isTrpcResponseParseError(error)).toBe(true);+ });+});++describe("fetchWithParseErrorStatus", () => {+ afterEach(() => {+ vi.unstubAllGlobals();+ });++ it("annotates the parse SyntaxError with the response HTTP status", async () => {+ vi.stubGlobal(+ "fetch",+ vi.fn(async () => new Response("", { status: 431 })),+ );++ const response = await fetchWithParseErrorStatus("http://localhost/x");+ await expect(response.json()).rejects.toMatchObject({+ name: "SyntaxError",+ responseStatus: 431,+ });+ });++ it("returns parsed JSON unchanged when the body is valid", async () => {+ vi.stubGlobal(+ "fetch",+ vi.fn(async () => new Response('{"ok":true}', { status: 200 })),+ );++ const response = await fetchWithParseErrorStatus("http://localhost/x");+ await expect(response.json()).resolves.toEqual({ ok: true });+ });+});+describe("getTrpcErrorCode / getTrpcErrorPath", () => {it("extracts the code and path from a tRPC server error", () => {const error = trpcServerError({diff --git a/web/src/utils/api.ts b/web/src/utils/api.tsindex 250e219ecc..c9fedcfd27 100644--- a/web/src/utils/api.ts+++ b/web/src/utils/api.ts@@ -170,6 +170,80 @@ export const isExpectedTrpcClientError = (error: unknown): boolean => {);};+// HTTP statuses returned when a request's URL/headers are too large for the+// browser or an upstream proxy. The response body is usually not a tRPC+// envelope, so these are otherwise hard to diagnose.+const REQUEST_TOO_LARGE_STATUSES = [414, 431];++type SyntaxErrorWithResponseStatus = SyntaxError & { responseStatus?: number };++/**+ * `@trpc/client` (11.13.4) rejects with the bare `SyntaxError` when+ * JSON-parsing a response body fails — the link only records the response+ * meta (and so the HTTP status) for fulfilled requests. This fetch wrapper+ * re-attaches the status to the parse `SyntaxError` so error classification+ * can tell an app-owned 414/431 failure from transport garbage.+ * Exported for tests.+ */+export const fetchWithParseErrorStatus: typeof fetch = async (input, init) => {+ const response = await fetch(input, init);+ const originalJson = response.json.bind(response);+ response.json = async () => {+ try {+ return await originalJson();+ } catch (error) {+ if (error instanceof SyntaxError) {+ (error as SyntaxErrorWithResponseStatus).responseStatus =+ response.status;+ }+ throw error;+ }+ };+ return response;+};++/** HTTP status of a failed tRPC response: from the annotated parse error+ * (`fetchWithParseErrorStatus`) or, when a link attached it, the response meta. */+const getResponseStatus = (error: TRPCClientError<any>): number | undefined => {+ const cause = getCause(error);+ if (+ cause instanceof SyntaxError &&+ typeof (cause as SyntaxErrorWithResponseStatus).responseStatus === "number"+ ) {+ return (cause as SyntaxErrorWithResponseStatus).responseStatus;+ }+ return error.meta?.response instanceof Response+ ? error.meta.response.status+ : undefined;+};++/**+ * True when `error` is a TRPCClientError caused by the tRPC client failing to+ * JSON-parse the HTTP response body (a `SyntaxError` cause, e.g. "JSON.parse:+ * unexpected character at line 1 column 1 of the JSON data" / "Unexpected end+ * of JSON input").+ *+ * Our tRPC handler always returns JSON, so a non-JSON body means something+ * between server and client replaced or truncated it (a proxy/LB error page,+ * an interrupted connection, an intercepting proxy). That is transport state,+ * not an app bug — the server owns the real signal. A parsed tRPC error+ * envelope (`error.data`) means the server DID answer with a real error+ * shape; those keep flowing to Sentry unchanged.+ *+ * Deliberately NOT matched: a parse failure on HTTP 414/431. That is the+ * app-owned oversized-GET-URL bug class (see `sendAsPostOption`) and must+ * keep capturing. Unknown status (no annotation, no meta) stays classified+ * as transport — the live client drops the response meta on parse failures,+ * which is why `fetchWithParseErrorStatus` exists.+ */+export const isTrpcResponseParseError = (error: unknown): boolean => {+ if (!(error instanceof TRPCClientError)) return false;+ if (error.data) return false;+ if (!(getCause(error) instanceof SyntaxError)) return false;+ const status = getResponseStatus(error);+ return status === undefined || !REQUEST_TOO_LARGE_STATUSES.includes(status);+};+/*** tRPC serializes query input into the GET URL. For reads whose input scales with* the number of rows (the `*.batchIO` I/O fetches), that URL grows large (~6KB at@@ -266,6 +340,20 @@ const handleTrpcError = (error: unknown, shouldSilenceError = false) => {httpStatus,},});+ } else if (isTrpcResponseParseError(error)) {+ // The response body was not JSON (proxy error page, truncated body) —+ // transport state, not an app bug. Breadcrumb instead of capture; the+ // toast below still renders. See `isTrpcResponseParseError`.+ addBreadcrumb({+ category: "trpc",+ type: "http",+ level: "warning",+ message: "Suppressed tRPC response parse error (not sent to Sentry)",+ data: {+ message: error.message,+ status: getResponseStatus(error),+ },+ });} else {// Real tRPC errors keep flowing to Sentry, now tagged by procedure/code// so they group and route instead of collapsing into one opaque bucket.@@ -324,11 +412,6 @@ const buildIdLink = (): TRPCLink<AppRouter> => () => {};};-// HTTP statuses returned when a request's URL/headers are too large for the-// browser or an upstream proxy. The response body is usually not a tRPC-// envelope, so these are otherwise hard to diagnose.-const REQUEST_TOO_LARGE_STATUSES = [414, 431];-// Logs request-size context to the console when a GET-routed query fails because// its URL was too large. tRPC serializes query input into the GET URL, so an// oversized input (a long list, a wide filter selection, ...) can trip HTTP 414@@ -346,10 +429,9 @@ const requestTooLargeDiagnosticsLink = (): TRPCLink<AppRouter> => () => {error(err) {const sentAsGet =op.type === "query" && op.context.sendAsPost !== true;- const status =- err.meta?.response instanceof Response- ? err.meta.response.status- : undefined;+ // Annotation-first (meta is dropped on JSON-parse failures — the+ // common shape of a real 414/431, whose body is empty/HTML).+ const status = getResponseStatus(err);if (sentAsGet &&@@ -446,10 +528,12 @@ export const api = createTRPCNext<AppRouter>({url: `${getBaseUrl()}/api/trpc`,transformer: superjson,methodOverride: "POST",+ fetch: fetchWithParseErrorStatus,}),false: httpLink({url: `${getBaseUrl()}/api/trpc`,transformer: superjson,+ fetch: fetchWithParseErrorStatus,}),}),// when condition is false, use batching@@ -457,6 +541,7 @@ export const api = createTRPCNext<AppRouter>({url: `${getBaseUrl()}/api/trpc`,transformer: superjson,maxURLLength: 2083, // avoid too large batches+ fetch: fetchWithParseErrorStatus,}),}),],diff --git a/web/src/utils/trpcErrorToast.tsx b/web/src/utils/trpcErrorToast.tsxindex c2e1e5bbc5..ad04b1caf9 100644--- a/web/src/utils/trpcErrorToast.tsx+++ b/web/src/utils/trpcErrorToast.tsx@@ -86,7 +86,7 @@ export const trpcErrorToast = (error: unknown) => {if (isResponseParseError(error)) {showErrorToast("Unexpected Response",- "The request could not be completed. We've been notified and are looking into it. Please try again or contact support if this persists.",+ "The request could not be completed. Please try again or contact support if this persists.","WARNING",);return;