Writing · JavaScript · Observability · Web
Your crash reporter is paging you for someone else's code
Extensions and third-party scripts throw into your page's error handlers. How to attribute a JavaScript stack trace to its real owner, and why the server decides.
A web app's global error handlers, window.onerror and the unhandledrejection event, hear everything that throws in the page's JavaScript world. That world isn't only yours. Wallet extensions inject scripts into it on purpose (they have to, to define window.ethereum). Popup blockers wrap fetch. Analytics tags you load yourself run there. So do userscripts, bookmarklets and whatever someone pastes into the console.
My crash reporter for DWEEB sends a small, content-free beacon to my server for every uncaught error, and the server turns a real crash into a page to my phone. Over a few months, four of those pages were for code I never shipped. This is how I learned to tell the difference, and the rules I ended up trusting.
Four pages that weren't mine
A stack overflow with no source URL
Safari reported Maximum call stack size exceeded.
with frames named @, Pk@ and Nk@, and no URL on any of them. JavaScriptCore renders code that has no script URL that way. The short names looked like minified code, which made me assume they were mine. To check, I rebuilt every bundle that had been deployed that week and compared the minified identifier frequencies against production. They matched (so local rebuilds name things the same way production does, a useful trick in itself), and no build contained those symbols. It was someone else's script overflowing its own stack.
MetaMask failing to connect to itself
The second was an unhandledrejection: Failed to connect to MetaMask
, with one frame at chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/…. That is MetaMask's page-world script. It starts a connection on load and drops the promise, which rejects after an extension-detection timeout. My app never touches window.ethereum. The interesting part was the build that reported it: a bundle about 25 days old, still running from a visitor's service-worker cache. Whatever I fixed in the client, that visitor's copy would keep reporting with the old rules for weeks. Only the server could stop it.
"Failed to fetch" from Google's own tag
The third read Failed to fetch, and some of its frames were "page" frames, which is why it paged. Those frames turned out to be Google's gtag.js, sending an analytics hit, underneath two frames from a popup blocker. The blocker wraps window.fetch and passes each request to an asynchronous inspector whose promise it never handles. When an analytics hit failed (blocked, or the visitor went offline), the dropped promise rejected too. gtag.js handles its own failures. I reproduced it exactly in headless Chrome with a replica of the hook, and without the hook the same failed requests produced nothing.
Safari's extension API wording
The fourth had no stack at all: Invalid call to runtime.sendMessage(). Tab not found.
That is WebKit's wording when an extension's content script calls a WebExtension API for a tab that's gone. Safari rejects those calls without a stack, so only the message attributes it. My app calls no extension APIs.
The rule: every location is someone else's
The tempting rule is the one several error-tracking tools use: look at the top frame, and if it belongs to an extension, drop the error. I rejected it, because it silences real bugs. Extensions that wrap browser APIs insert their frames above yours. If my code calls history.pushState with something invalid while an extension wraps pushState, the top frame is the extension's, and the bug is mine.
What I use instead:
- Extract every location in the stack: each
://together with the whole scheme before it, compared case-insensitively. A stack line without a location (<anonymous>,[native code],async Promise.all (index 0)) is ignored. - The error is foreign only if there is at least one location and all of them belong to someone else. A single frame of mine anywhere in the stack keeps it mine.
- "Someone else" means an extension scheme (
chrome-extension,moz-extension,safari-web-extension,safari-extension, andwebkit-masked-url, which is how Safari masks every extension URL), Firefox's<anonymous code>(its name for page-world extension scripts), or a short list of third-party script hosts I load but don't ship.
const EXTENSION_SCHEMES = new Set([
"chrome-extension", "moz-extension", "safari-web-extension",
"safari-extension", "webkit-masked-url",
]);
const THIRD_PARTY_HOSTS = new Set(["www.googletagmanager.com"]);
function locations(stack) {
const found = [];
for (const m of stack.matchAll(/([A-Za-z0-9+.-]*):\/\/([^\s/)]*)(\/?)/g)) {
found.push({ scheme: m[1].toLowerCase(), host: m[2].toLowerCase(), slash: m[3] === "/" });
}
if (stack.includes("<anonymous code>")) found.push({ scheme: "firefox-extension" });
return found;
}
function isForeign(stack) {
const locs = locations(stack);
return locs.length > 0 && locs.every((l) =>
EXTENSION_SCHEMES.has(l.scheme) || l.scheme === "firefox-extension" ||
(l.scheme === "https" && l.slash && THIRD_PARTY_HOSTS.has(l.host)));
}
Some details in that sketch are deliberate. A scheme like blob:https or webpack-internal reads as mine, because the comparison takes the whole scheme run, not its tail. A third-party host only counts when the host is followed directly by a slash, so a longer host that merely starts with the same name, a URL with a port or credentials, plain http, or the host appearing inside a path all read as mine. When in doubt, the rule falls on the side that pages.
What still pages, on purpose
- A rejection with no locations at all. When my own
fetchfails and nothing catches it, Chrome rejects with a bareTypeErrorthat names no frame. Treating "no location" as foreign would hide that. - Anything caught by my error boundary or the boot path. The foreign rule only applies to the two window-level traps. If an extension manages to take down rendering, that is an outage, whoever's fault it is.
- Message lists. I never drop an error by its wording alone, with one exception (the stackless WebKit extension-API wording above, which my app cannot produce).
Cannot read properties of null (reading 'type')
is a common extension error and also, word for word, a bug I could write tomorrow.
The truncation trap
Beacons are small on purpose: the stack travels as at most six lines and 800 characters. V8 spends the first line on the Name: message header, and some messages are long or span several lines (validation libraries pretty-print, DataCloneError quotes source code). Those pushed every frame out of the window. Since a stack that names no location used to count as unattributed, my own crash in Chrome could be silenced simply by having a verbose message.
The fix was to have the client judge the full stack before trimming it, and then, when the trimmed window would contain no frame of mine, to promote the first frame of mine into it with a marker for the skipped lines:
TypeError: Failed to execute 'structuredClone' on 'Window': …
at chrome-extension://abc/inject.js:1:2041
... 14 lines skipped ...
at https://example.com/assets/editor-3f9a1c.js:2:18803
The promoted line is budgeted first, so no later clamp can cut its location in half. And clamps never split a UTF-16 surrogate pair: an orphaned half serializes as an escape that strict JSON parsers refuse, and the whole beacon is lost.
Send evidence, not a verdict
The easiest implementation would have been a boolean in the beacon: foreign: true. I didn't do it, and this is the lesson I'd pass on to anyone with a service worker. Clients run old bundles for a long time. Every rule you put in the client is frozen into each deployed version for as long as some visitor keeps that version cached, and you can't fix it. The MetaMask page came from a bundle more than three weeks old.
So the client sends evidence (the kind of error and a stack window that includes a frame of mine if one exists), and the server decides. The rules exist in both places, but the server is the authority, and both implementations are tested against one shared file of hand-written vectors so they can't drift apart. When I add an extension scheme, I add it to the client, the server and the vector file in one change, and only after a real beacon shows it.
Foreign errors aren't thrown away. They're logged at info level with a frames= field (extension, third-party, page or mixed), so I can still count them. And real crashes carry the same field, so a page that says frames=mixed tells me up front that someone else's code was in the stack beside mine.
What I'd tell someone setting up error reporting
- Your global error handlers report code you don't own. Plan for it on day one.
- Attribute by every location in the stack, not the top frame.
- Keep a stack with no locations on the paging side.
- Judge the full stack before truncating it.
- Put the decision on the server, because old clients never fully go away.
- Add a rule only when a real report shows you need it, and keep a test vector for it.
