Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🔊 Suppress benign AbortError noise from tRPC request cancellation (#2980)
posthog-eu[bot]c89bc7ddf1ee
diff --git a/apps/web/src/trpc/client/provider.tsx b/apps/web/src/trpc/client/provider.tsxindex 3ec105941..059773233 100644--- a/apps/web/src/trpc/client/provider.tsx+++ b/apps/web/src/trpc/client/provider.tsx@@ -19,6 +19,15 @@ function isTRPCClientError(error: Error): error is TRPCClientError<AppRouter> {return error instanceof TRPCClientError;}+// A request cancelled on unmount or navigation surfaces as an aborted tRPC+// request with no server response. Nothing broke, so it must not raise a+// "Unable to reach the server" toast.+function isAbortError(error: TRPCClientError<AppRouter>) {+ return (+ error.cause instanceof DOMException && error.cause.name === "AbortError"+ );+}+export function TRPCProvider(props: { children: React.ReactNode }) {const { t } = useTranslation();const [queryClient] = useState(() => {@@ -27,6 +36,11 @@ export function TRPCProvider(props: { children: React.ReactNode }) {return;}+ // A cancelled request is not a failure — never warn about connectivity+ if (isAbortError(error)) {+ return;+ }+// Missing error data means no usable server response: the request// failed in transit or the response was not a tRPC error envelope// (e.g. an HTML error page from a proxy)diff --git a/packages/posthog/src/client.ts b/packages/posthog/src/client.tsindex 925f4773e..2c3d87263 100644--- a/packages/posthog/src/client.ts+++ b/packages/posthog/src/client.ts@@ -4,6 +4,7 @@ import posthog from "posthog-js";export { useFeatureFlagEnabled } from "posthog-js/react";import {+ isAbortError,isGlobalPrivacyControlEnabled,isInjectedExtensionException,} from "./utils";@@ -26,7 +27,7 @@ if (typeof window !== "undefined" && process.env.NEXT_PUBLIC_POSTHOG_API_KEY) {before_send: (event) => {if (event?.event === "$exception" &&- isInjectedExtensionException(event)+ (isInjectedExtensionException(event) || isAbortError(event))) {return null;}diff --git a/packages/posthog/src/utils.test.ts b/packages/posthog/src/utils.test.tsindex 7113f036e..6041681d2 100644--- a/packages/posthog/src/utils.test.ts+++ b/packages/posthog/src/utils.test.ts@@ -1,5 +1,10 @@+import type { CaptureResult } from "posthog-js";import { describe, expect, it } from "vitest";-import { getPostHogCookieName, parsePostHogCookieDistinctId } from "./utils";+import {+ getPostHogCookieName,+ isAbortError,+ parsePostHogCookieDistinctId,+} from "./utils";describe("getPostHogCookieName", () => {it("builds the posthog-js persistence cookie name", () => {@@ -7,6 +12,54 @@ describe("getPostHogCookieName", () => {});});+describe("isAbortError", () => {+ const event = (exceptionList: unknown) =>+ ({+ properties: { $exception_list: exceptionList },+ }) as unknown as CaptureResult;++ it("matches an aborted tRPC request", () => {+ expect(+ isAbortError(+ event([+ { type: "TRPCClientError", value: "The operation was aborted. " },+ {+ type: "DOMException",+ value: "AbortError: The operation was aborted. ",+ },+ ]),+ ),+ ).toBe(true);+ });++ it("matches a bare AbortError DOMException", () => {+ expect(+ isAbortError(+ event([+ {+ type: "DOMException",+ value: "AbortError: The user aborted a request.",+ },+ ]),+ ),+ ).toBe(true);+ });++ it("does not match an unrelated exception", () => {+ expect(+ isAbortError(+ event([{ type: "TypeError", value: "x is not a function" }]),+ ),+ ).toBe(false);+ });++ it("does not match when the exception list is missing", () => {+ expect(isAbortError({ properties: {} } as unknown as CaptureResult)).toBe(+ false,+ );+ });+});+describe("parsePostHogCookieDistinctId", () => {it("parses a URL-encoded JSON cookie value", () => {const value = encodeURIComponent(diff --git a/packages/posthog/src/utils.ts b/packages/posthog/src/utils.tsindex fee9ca296..84a2b9c72 100644--- a/packages/posthog/src/utils.ts+++ b/packages/posthog/src/utils.ts@@ -86,3 +86,30 @@ export function isInjectedExtensionException(event: CaptureResult) {),);}++/**+ * A request that is aborted (React Query cancels an in-flight query on unmount+ * or navigation) surfaces as an `AbortError`. When it wraps a tRPC request, the+ * `$exception_list` holds a `TRPCClientError` ("The operation was aborted.")+ * chained to a `DOMException` whose value starts with "AbortError:". Nothing+ * broke for the user, so this is autocapture noise, not a real error.+ */+const ABORT_NOISE_VALUE_PATTERN =+ /^AbortError\b|operation was aborted|user aborted a request/i;++export function isAbortError(event: CaptureResult) {+ const exceptionList = event.properties?.$exception_list as+ | Array<{ type?: string; value?: string }>+ | undefined;++ if (!Array.isArray(exceptionList)) {+ return false;+ }++ return exceptionList.some(+ (exception) =>+ exception?.type === "AbortError" ||+ (typeof exception?.value === "string" &&+ ABORT_NOISE_VALUE_PATTERN.test(exception.value)),+ );+}