mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 10:44:52 +01:00
fix(ui): name the kind of value an uncaught browser error threw (#23346)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The board UI reports uncaught browser errors to Sentry when a deployment sets a DSN > - A React event handler that throws a string reaches Sentry through `window.onerror` with no stack of its own > - The event then shows only one frame: React's event dispatch (`processDispatchQueue`). It does not show the handler that threw the value, or the kind of the value > - Breadcrumbs and the user agent are not sent by design, so we cannot tell a scripted browser session from a person > - This pull request adds two bounded facts to these reports. It does not copy the thrown value > - The benefit is that the next occurrence tells us if a string or a stackless `Error` was thrown, and if a WebDriver-controlled browser threw it ## Linked Issues or Issue Description **What happened?** One browser event on a hosted Paperclip page reported `Error: 844g8`. The mechanism was `auto.browser.global_handlers.onerror` (unhandled). The only stack frame was React's event dispatch in the main bundle, at the `catch` that reports a listener's throw. The event had no breadcrumbs and no user agent. The source location was a cloud data center region. **Expected behavior** A report that lets a maintainer find what threw the value, or at least identify the class of the throw. **Steps to reproduce** 1. Enable browser Sentry monitoring. 2. Throw a string (for example `throw "844g8"`) from a React event handler, or call `window.onerror` with a string `error` argument. 3. Look at the Sentry event. It shows `Error: 844g8` with one synthetic frame at the reported location, and there is no other attribution. **Paperclip version or commit** The event came from release `506211b9176058ae06622b2bc1150d4bb2ea2e19` (hosted production bundle). This change is based on current `master`. No duplicate or related open pull request touches this path. I searched for the error text, for `onerror`, and for non-`Error` throws. ## What Changed - `ui/src/lib/browser-error-context.ts`: Add `classifyThrownValue()`. It returns `error`, `error_stackless`, or the primitive type. It checks only if an `Error` stack has frames. It does not coerce or copy the value. - `ui/src/lib/browser-error-context.ts`: Add `browser_state.automation` from `navigator.webdriver === true`. A failed read leaves out the field and keeps the other state. - `ui/src/lib/sentry.ts`: In `beforeSend`, tag `window.onerror` reports with `browser_thrown_kind`. Rejections and caught exceptions do not get this tag. - `doc/observability.md`: Document the new tag and field. - Tests use the real `@sentry/browser` SDK for the onerror path, and unit tests for the browser state. ## Verification - `cd ui && npx vitest run src/lib/sentry.test.ts src/lib/browser-error-context.test.ts`: 68 passed. - Red/green: with the source change removed, the two new `sentry.test.ts` tests fail (`browser_thrown_kind` is `undefined`, and `browser_state.automation` is missing). With the change, they pass. - The new real-SDK test throws the string `844g8` through `window.onerror`. The SDK produces the same shape as the production event: exception value `844g8`, mechanism `auto.browser.global_handlers.onerror`. The event now also has `browser_thrown_kind: string`. - `cd ui && node ../scripts/run-tsc.mjs -b`: pass. - `pnpm verify` exited 2 (broader verification required, from global CI inputs). Required CI supplies that coverage. ## Risks - Low risk. The change adds one tag and one boolean to browser Sentry events. It does not drop, group, or suppress any event. - Every read is in a `try` block, so a failed read cannot replace or discard the original error. - `navigator.webdriver` is a standard boolean. It does not identify a person or a tenant. - This is a diagnostic. It does not fix a defect. The root cause needs a future event with these facts. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude Code with tool use and shell execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: devinfoley <139239+devinfoley@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
5 files changed
+139
-2
No files matched your search
@@ -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.
|
||||
|
||||
@@ -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<string, unknown>).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({});
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<string, unknown>[] = [];
|
||||
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<string, unknown>).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<string, unknown>).webdriver;
|
||||
await Sentry.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("does not tag a rejection or a caught exception with a thrown-value kind", async () => {
|
||||
const captured: Record<string, unknown>[] = [];
|
||||
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);
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user