Chrome extension: how to trap/handle content-script errors globally?

5 months after asking this question, I’m now pretty convinced there isn’t currently an easy way to global trap/handle Javascript errors occurring in a content script. As such,

  • I’ve created a Chrome bug report: window.onerror should contain useful info for thrown error in extension content script. Feel free to chip in with useful comments.
  • If you need to globally trap content script errors, your best bet is to assume window.onerror is busted for now in content scripts. Instead, wrap all Javascript code in try-catch blocks, which gives you access to useful error information. This is trickier than it sounds, as you have to handle both synchronous code (easy) and asynchronous-callback code (trickier). Take a look at this article for starters, and good luck!

Leave a Comment