diff --git a/doc/observability.md b/doc/observability.md index 68a76b555f8..fbc32a552af 100644 --- a/doc/observability.md +++ b/doc/observability.md @@ -473,10 +473,19 @@ All browser error reports, including global promise rejections, include: - `browser_build_mode`: `development` or `production`, from the loaded bundle. - `browser_rejection_kind`: the primitive type of an unhandled rejected value (or `null`). The diagnostic does not read object properties or copy the value. +- `browser_thrown_kind`: for an uncaught error that reaches `window.onerror`, + `error` (an `Error` with stack frames), `error_stackless` (an `Error` with no + frames), or the primitive type of a thrown non-`Error` value. A thrown string + has no stack, so Sentry shows only the frame where the browser reported it, + such as React's event dispatch. The diagnostic does not copy the value. - `browser_state`: document readiness, visibility, and a boolean indicating the `translated-ltr` or `translated-rtl` root class used by browser translation. The marker is evidence of DOM translation, not proof of the error's cause; its absence does not exclude other translators or DOM-changing extensions. + `automation` is `true` when `navigator.webdriver` reports a WebDriver-controlled + browser. A value of `false` does not prove that a person was using the page. + `error` means at least one stack line has the shape of a V8, Firefox, or + Safari frame; message text alone does not count. Boundary state is captured at the failure, before asynchronous reporting. Global reports without that snapshot read document state before sending. diff --git a/ui/src/lib/browser-error-context.test.ts b/ui/src/lib/browser-error-context.test.ts index 9259dd1af84..6178c4784e1 100644 --- a/ui/src/lib/browser-error-context.test.ts +++ b/ui/src/lib/browser-error-context.test.ts @@ -5,6 +5,7 @@ import { __resetPageDiagnostics, buildBrowserErrorContext, buildQueryErrorContext, + classifyThrownValue, queryKindFor, readPageLifecycle, sanitizeComponentStack, @@ -88,7 +89,7 @@ unrecognized private content`; const result = buildBrowserErrorContext({ boundary: "app" }); document.documentElement.classList.add("translated-ltr"); expect(result.contexts.browser_state).toEqual({ - ready_state: "interactive", visibility_state: "hidden", translation_marker: false, + ready_state: "interactive", visibility_state: "hidden", translation_marker: false, automation: false, }); }); @@ -99,12 +100,48 @@ unrecognized private content`; tags: { react_error_boundary: "app" }, contexts: { react: { componentStack: "\n at TaskDetail" }, - browser_state: { visibility_state: document.visibilityState }, + browser_state: { visibility_state: document.visibilityState, automation: false }, page_lifecycle: expect.objectContaining({ pagehide_seen: false }), }, }); }); + function withWebdriver(get: () => unknown, run: () => void) { + Object.defineProperty(navigator, "webdriver", { configurable: true, get }); + try { + run(); + } finally { + delete (navigator as unknown as Record).webdriver; + } + } + + it("records whether a WebDriver session controls the browser", () => { + withWebdriver(() => true, () => { + expect(buildBrowserErrorContext({ boundary: "app" }).contexts.browser_state).toHaveProperty("automation", true); + }); + }); + + it("keeps other browser state when the automation flag cannot be read", () => { + withWebdriver(() => { throw new Error("unavailable"); }, () => { + const state = buildBrowserErrorContext({ boundary: "app" }).contexts.browser_state; + expect(state).not.toHaveProperty("automation"); + expect(state).toHaveProperty("translation_marker", false); + }); + }); + + it("classifies a thrown value by its kind and whether its stack has frames", () => { + const withStack = (stack: string) => Object.assign(new Error("failure"), { stack }); + expect(classifyThrownValue("844g8")).toBe("string"); + expect(classifyThrownValue(null)).toBe("null"); + expect(classifyThrownValue(undefined)).toBe("undefined"); + expect(classifyThrownValue(withStack("Error: failure\n at handler (https://tenant.example/a.js:1:2)"))).toBe("error"); + expect(classifyThrownValue(withStack("handler@https://tenant.example/a.js:1:2"))).toBe("error"); + expect(classifyThrownValue(withStack("global code@https://tenant.example/a.js:3:4\n"))).toBe("error"); + expect(classifyThrownValue(withStack("Error: failure"))).toBe("error_stackless"); + expect(classifyThrownValue(withStack("Error: failed at 12:34:56"))).toBe("error_stackless"); + expect(classifyThrownValue(withStack(""))).toBe("error_stackless"); + }); + it("does not require a browser document", () => { vi.stubGlobal("document", undefined); expect(buildBrowserErrorContext({ boundary: "app" }).contexts.browser_state).toEqual({}); diff --git a/ui/src/lib/browser-error-context.ts b/ui/src/lib/browser-error-context.ts index 0aea37b1693..507d73e6351 100644 --- a/ui/src/lib/browser-error-context.ts +++ b/ui/src/lib/browser-error-context.ts @@ -29,6 +29,7 @@ export function readBrowserErrorState() { ready_state?: "loading" | "interactive" | "complete"; visibility_state?: "visible" | "hidden"; translation_marker?: boolean; + automation?: boolean; } = {}; if (typeof document !== "undefined") { // Each read is optional. A DOM wrapper or extension must not prevent the @@ -46,6 +47,9 @@ export function readBrowserErrorState() { try { browserState.translation_marker = readPageTranslationMarker(); } catch { /* diagnostic unavailable */ } + try { + browserState.automation = navigator.webdriver === true; + } catch {} } return browserState; } @@ -100,6 +104,33 @@ export function classifyRejectionReason(reason: unknown) { return reason === null ? "null" : typeof reason; } +export type ThrownValueKind = + | "error" + | "error_stackless" + | "string" + | "number" + | "boolean" + | "bigint" + | "symbol" + | "undefined" + | "null" + | "object" + | "function"; + +const STACK_FRAME_LINE = /^\s+at \S|^[^@\n]*@\S+:\d+:\d+\r?$/m; + +export function classifyThrownValue(value: unknown): ThrownValueKind { + try { + if (value instanceof Error) { + const stack: unknown = value.stack; + return typeof stack === "string" && STACK_FRAME_LINE.test(stack) ? "error" : "error_stackless"; + } + } catch { + return "object"; + } + return value === null ? "null" : typeof value; +} + function classifyRejectionPromise(event: PromiseRejectionEvent): RejectionPromiseKind { try { const promise: unknown = event.promise; diff --git a/ui/src/lib/sentry.test.ts b/ui/src/lib/sentry.test.ts index 6adfbec318b..25842e9f9dd 100644 --- a/ui/src/lib/sentry.test.ts +++ b/ui/src/lib/sentry.test.ts @@ -710,6 +710,55 @@ describe("captured event shape against the real @sentry/browser SDK", () => { } }); + it("names what kind of value an uncaught global error threw, and whether the browser is automated", async () => { + const captured: Record[] = []; + const Sentry = await initRealSentryForTest((event) => { captured.push(event); }); + const report = (thrown: unknown) => window.onerror?.call( + window, + `Uncaught ${String(thrown)}`, + "https://tenant.example/assets/index-private.js", + 449, + 141515, + thrown as Error, + ); + const stackless = new Error("stackless failure"); + stackless.stack = "Error: stackless failure"; + Object.defineProperty(navigator, "webdriver", { configurable: true, get: () => true }); + try { + expect(window.onerror).toBeTypeOf("function"); + report("844g8"); + report(new Error("ordinary failure")); + report(stackless); + await Sentry.flush(2000); + expect(captured).toHaveLength(3); + expect(captured.map((event) => (event.tags as Record).browser_thrown_kind)) + .toEqual(["string", "error", "error_stackless"]); + expect(captured[0]).toMatchObject({ + contexts: { browser_state: { automation: true } }, + exception: { values: [{ value: "844g8", mechanism: { type: "auto.browser.global_handlers.onerror" } }] }, + }); + for (const event of captured) expect(event).not.toHaveProperty("request"); + } finally { + delete (navigator as unknown as Record).webdriver; + await Sentry.close(); + } + }); + + it("does not tag a rejection or a caught exception with a thrown-value kind", async () => { + const captured: Record[] = []; + const Sentry = await initRealSentryForTest((event) => { captured.push(event); }); + try { + window.onunhandledrejection?.call(window, { reason: "rejected text" } as PromiseRejectionEvent); + Sentry.captureException(new Error("caught by app code")); + await Sentry.flush(2000); + expect(captured).toHaveLength(2); + for (const event of captured) expect(event).not.toHaveProperty("tags.browser_thrown_kind"); + expect(captured[1]).toHaveProperty("contexts.browser_state.automation", false); + } finally { + await Sentry.close(); + } + }); + describe("posthog-js request timeouts", () => { function postHogTimeoutError(message = "PostHog request timed out after 3000ms"): Error { const error = new Error(message); diff --git a/ui/src/lib/sentry.ts b/ui/src/lib/sentry.ts index 7b62bb9188f..1368c4d3618 100644 --- a/ui/src/lib/sentry.ts +++ b/ui/src/lib/sentry.ts @@ -51,6 +51,7 @@ import { buildBrowserErrorContext, buildQueryErrorContext, classifyRejectionReason, + classifyThrownValue, readBrowserErrorState, readPageLifecycle, startPageDiagnostics, @@ -221,6 +222,13 @@ export function isPostHogRequestTimeout(reason: unknown): boolean { } } +function isGlobalOnError(event: { exception?: { values?: Array<{ mechanism?: { type?: string } }> } }) { + return event.exception?.values?.some((value) => + value.mechanism?.type === "onerror" + || value.mechanism?.type === "auto.browser.global_handlers.onerror", + ) ?? false; +} + /** * Build the `Sentry.init` options object. A pure function, split out from * `initBrowserErrorMonitoring` so a test can call it with the real @@ -271,6 +279,9 @@ export function buildBrowserSentryInitOptions( // whose module holds the innermost such frame, never the URL. const pluginModule = readPluginModuleOrigin(event.exception?.values); if (pluginModule) event.tags.browser_plugin_module = pluginModule; + if (isGlobalOnError(event)) { + event.tags.browser_thrown_kind = classifyThrownValue(hint.originalException); + } if (isUnhandledRejection) { // The SDK supplies the rejected value directly. Classify without // reading object properties, coercing strings, or copying its value.