DEV Community

Mohamed Gado
Mohamed Gado

Posted on Edited on Originally published at mohamedgado.com

__gCrWeb, __firefox__ and Script error.: filtering errors your site never threw

Errors that mention __gCrWeb, __firefox__, window.ethereum or webkit.messageHandlers come from code that the browser or an extension injected into your page, not from your bundle. So does a bare Script error. with no file name. My PostHog error board showed 21 exceptions in one week and it flagged the week as BAD. None of them came from my code: 11 were an opaque Script error., 4 were Firefox for iOS and a crypto wallet, and 6 were a third-party chat widget that a content blocker stopped from loading. I now drop the injected and opaque ones in the client before they become $exception events, record the blocked widget as a plain event, and suppress the old issues on the board.

Where do __gCrWeb and __firefox__ errors come from?

Mobile browsers on iOS can't ship their own engine. Chrome and Firefox for iOS run on WebKit and add their features (autofill, translate, reader mode, password managers) by injecting JavaScript into every page you open:

Global in the message Who injects it
__gCrWeb Chrome for iOS
__firefox__ Firefox for iOS
window.ethereum Crypto wallet extensions and wallet in-app browsers
webkit.messageHandlers In-app WebViews (social apps, mail apps) bridging to native code

When that injected script throws, the browser fires the page's window error event, the same one your error tracker listens to. The tracker can't tell whose code it was, so it records the error against your page.

How do you tell an injected error from your own bug?

Look at where the error claims to be. In my case the injected errors pointed at the page URL itself, line 1, columns 12 to 19. Line 1 of an HTML page is <!DOCTYPE html>. No script of mine runs there. A real bug in your code points at one of your JavaScript chunks.

Then group the events by visitor and browser before you call it a user problem. My 21 exceptions came from two visitors:

  • One visitor on Firefox for iOS with a wallet installed, on one page, produced the __firefox__ and window.ethereum errors.
  • Another source produced 11 Script error. events that week and 60 over 14 days. None of them had a file name.

A HogQL query in PostHog that groups $exception events by browser and source shows this in one table:

SELECT properties.$browser AS browser,
       properties.$exception_source AS source,
       count() AS c
FROM events
WHERE event = '$exception'
  AND timestamp > now() - INTERVAL 7 DAY
GROUP BY browser, source
ORDER BY c DESC
Enter fullscreen mode Exit fullscreen mode

Two people with nothing in common with each other and nothing in common with the code: that is noise.

What does "Script error." with no file name mean?

Browsers hide the details of errors thrown by scripts from another origin, unless that script is loaded with crossorigin and its server sends CORS headers. All your error handler receives is the message Script error., an empty filename, and line and column 0. Without a file you can't act on it, and if your own scripts are same-origin, it can't be them.

My site serves its own JavaScript from the same origin, so an opaque Script error. with no file name is always someone else's script.

How to filter injected errors before they reach your tracker

The filter lives in the one error listener the site's tracker uses:

// Globals a browser or an extension injects into every page: Firefox for iOS (__firefox__),
// Chrome for iOS (__gCrWeb), wallets (window.ethereum), in-app WebViews (webkit.messageHandlers).
const INJECTED = /__firefox__|__gCrWeb|window\.ethereum|webkit\.messageHandlers/

export function isInjectedNoise(message: string, filename: string): boolean {
  if (/^Script error\.?$/.test(message.trim()) && !filename) return true
  return INJECTED.test(message)
}

window.addEventListener('error', (e) => {
  if (isInjectedNoise(String(e.message ?? ''), e.filename ?? '')) return
  report(e.error ?? e.message, false, e.filename)
})
Enter fullscreen mode Exit fullscreen mode

Two choices in there matter:

  • Script error. is dropped only when the file name is empty. If one of my own scripts ever reports Script error. with a file attached, it still gets through.
  • The regex matches the message, not the stack. Injected errors name the global in the message (window.__firefox__.reader, window.ethereum.selectedAddress), and some of them have no useful stack at all.

isInjectedNoise is a pure function, so it has its own test. The messages in it are the real ones from the board:

test('opaque Script error and injected globals are dropped, our errors are kept', () => {
  const page = 'https://www.mohamedgado.com/x'
  const chunk = 'https://www.mohamedgado.com/_next/static/chunks/a.js'
  assert.equal(isInjectedNoise('Script error.', ''), true)
  assert.equal(isInjectedNoise("Can't find variable: __firefox__", page), true)
  assert.equal(isInjectedNoise("undefined is not an object (evaluating 'window.__firefox__.reader')", page), true)
  assert.equal(isInjectedNoise("undefined is not an object (evaluating 'window.ethereum.selectedAddress = undefined')", page), true)
  assert.equal(isInjectedNoise('Script error.', chunk), false)
  assert.equal(isInjectedNoise("Cannot read properties of undefined (reading 'map')", chunk), false)
})
Enter fullscreen mode Exit fullscreen mode

The last two lines are the important ones: a Script error. that names one of my chunks, and an ordinary bug, must both still be reported. I ran the test against the tracker without the filter and saw it fail before I kept the change.

Suppress, don't resolve

The board already held three issues for this noise. I marked them suppressed, not resolved, with a note that names where the filter lives. In PostHog, as in most error trackers, a resolved issue reopens on its next event. Old tabs and cached pages keep sending events for a while after a deploy, so resolved noise comes back and someone investigates it again. A suppressed issue stays closed.

Takeaways

  • An error that names __gCrWeb, __firefox__, window.ethereum or webkit.messageHandlers is the browser's code or an extension's, not yours.
  • Script error. with no file name is a cross-origin script you can't see into. If your scripts are same-origin, it isn't yours.
  • Group exceptions by browser and source before you count them as users. Here, the board counted only 2 visitors behind them.
  • Filter in the client with a tested pure function, then suppress the old issues instead of resolving them.

A blocked third-party script is an expected failure, not a bug, so it belongs in a counted product event ("chat widget failed to load") rather than an exception. The real production error this board did catch was React #418 from reading matchMedia during the first render.

Top comments (0)