diff --git a/ui/src/lib/sentry.test.ts b/ui/src/lib/sentry.test.ts index 0700e58d804..b37aacb85ef 100644 --- a/ui/src/lib/sentry.test.ts +++ b/ui/src/lib/sentry.test.ts @@ -616,6 +616,69 @@ describe("captured event shape against the real @sentry/browser SDK", () => { } }); + describe("posthog-js request timeouts", () => { + function postHogTimeoutError(message = "PostHog request timed out after 3000ms"): Error { + const error = new Error(message); + Object.defineProperty(error, "name", { value: "AbortError" }); + return error; + } + + async function initCountingSentry() { + const { buildBrowserSentryInitOptions } = await importFreshSentry(); + const Sentry = await import("@sentry/browser"); + const send = vi.fn(async () => ({})); + Sentry.init({ + ...buildBrowserSentryInitOptions(DSN, "production"), + transport: () => ({ send, flush: async () => true }), + }); + const sentEvents = () => send.mock.calls.map((call: unknown[]) => JSON.stringify(call[0])) + .filter((envelope) => envelope.includes("\"type\":\"event\"")); + return { Sentry, sentEvents }; + } + + function rejectUnhandled(reason: unknown) { + window.onunhandledrejection?.call(window, { reason } as PromiseRejectionEvent); + } + + it("drops posthog-js's own timeout when it escapes as an unhandled rejection", async () => { + const { Sentry, sentEvents } = await initCountingSentry(); + try { + rejectUnhandled(postHogTimeoutError()); + rejectUnhandled(postHogTimeoutError("PostHog request timed out")); + await Sentry.flush(2000); + expect(sentEvents()).toHaveLength(0); + } finally { + await Sentry.close(); + } + }); + + it.each([ + ["another AbortError", () => Object.assign(new Error("The user aborted a request."), { name: "AbortError" })], + ["the same text on a plain Error", () => new Error("PostHog request timed out after 3000ms")], + ["a string reason with the same text", () => "PostHog request timed out after 3000ms"], + ])("still reports %s", async (_label, makeReason) => { + const { Sentry, sentEvents } = await initCountingSentry(); + try { + rejectUnhandled(makeReason()); + await Sentry.flush(2000); + expect(sentEvents()).toHaveLength(1); + } finally { + await Sentry.close(); + } + }); + + it("still reports the same error when Paperclip code captures it", async () => { + const { Sentry, sentEvents } = await initCountingSentry(); + try { + Sentry.captureException(postHogTimeoutError()); + await Sentry.flush(2000); + expect(sentEvents()).toHaveLength(1); + } finally { + await Sentry.close(); + } + }); + }); + it.each([ ["staging", "staging"], ["production", "production"], diff --git a/ui/src/lib/sentry.ts b/ui/src/lib/sentry.ts index 52481d6dcbc..536355d7a2c 100644 --- a/ui/src/lib/sentry.ts +++ b/ui/src/lib/sentry.ts @@ -191,6 +191,18 @@ function enqueueCapture( }); } +const POSTHOG_REQUEST_TIMEOUT_MESSAGE = /^PostHog request timed out(?: after \d+ms)?$/; + +export function isPostHogRequestTimeout(reason: unknown): boolean { + try { + return reason instanceof Error + && reason.name === "AbortError" + && POSTHOG_REQUEST_TIMEOUT_MESSAGE.test(reason.message); + } catch { + return false; + } +} + /** * Build the `Sentry.init` options object. A pure function, split out from * `initBrowserErrorMonitoring` so a test can call it with the real @@ -217,7 +229,10 @@ export function buildBrowserSentryInitOptions( value.mechanism?.type === "onunhandledrejection" || value.mechanism?.type === "auto.browser.global_handlers.onunhandledrejection", ) ?? false; - if (isUnhandledRejection && isIntentionalAbortReason(hint.originalException)) { + if ( + isUnhandledRejection + && (isIntentionalAbortReason(hint.originalException) || isPostHogRequestTimeout(hint.originalException)) + ) { takeRejectionEventSnapshot(classifyRejectionReason(hint.originalException)); return null; }