title: Chrome extension: how to trap/handle content-script errors globally?
body: <p><strong>In a Chrome extension, is there a way to globally trap/handle Javascript errors occurring in a content-script?</strong> (e.g. for submission to a Javascript error tracking service like bugsnag)</p>
  
  <p>Ideally, I would setup a global <code>window.onerror</code> handler at the top of the content script. But it's not working properly in Chrome 40: the error is caught, but the information supplied is useless: a message of 'Script error' and no url, lineNumber, column or error object with stack.</p>
  
  <p>I created a test extension to show this busted behaviour for content scripts. Details below. Interesting findings:</p>
  
  <ul>
  <li><code>window.onerror</code> works correctly in a background script, with full error information</li>
  <li><code>window.onerror</code> on the hosting webpage can also <a href="https://code.google.com/p/chromium/issues/detail?id=225513" rel="noreferrer">see the error thrown by an extension content script</a> (but the error doesn't contain any useful info either)</li>
  </ul>
  
  <h3>Reproducing busted window.onerror for Chrome extension content script.</h3>
  
  <p>In a new folder, create <code>manifest.json</code>, <code>content-script.js</code> and <code>background-script.js</code>. Then load into Chrome via Window > Extensions > Load unpacked Extension.</p>
  
  <ul>
  <li>To see the busted window.onerror info for a content-script error, reload a webpage and look at the devtools console</li>
  </ul>
  
  <h3>manifest.json</h3>
  
  <pre><code>{
      "name": "Chrome extension content-script errors test",
      "manifest_version": 2,
      "version": "0.0.1",
      "background": {
        "scripts": [ "background-script.js" ]
      },
      "content_scripts": [{
          "matches": ["&lt;all_urls&gt;"],
          "js": ["content-script.js"]
      }]
  }
  </code></pre>
  
  <h3>content-script.js</h3>
  
  <pre><code>window.onerror = function (errorMsg, url, lineNumber, column, errorObj) {
      console.log('Caught content script error');
      console.log('errorMsg: ' + errorMsg);
      console.log('url: ' + url);
      console.log('lineNumber: ' + column);
      console.log('column: ' + column);
      console.log('errorObj follows:');
      console.log(errorObj);
      return true;
  };
  
  console.log('I am a content script, about to throw an error');
  throw new Error('Is this error caught?');
  </code></pre>
  
  <h3>background-script.js</h3>
  
  <pre><code>window.onerror = function (errorMsg, url, lineNumber, column, errorObj) {
      console.log('Caught background script error');
      console.log('errorMsg: ' + errorMsg);
      console.log('url: ' + url);
      console.log('lineNumber: ' + column);
      console.log('column: ' + column);
      console.log('errorObj follows:');
      console.log(errorObj);
      return true;
  };
  
  //To see nice window.onerror behaviour for background script..
  //Uncomment 2 lines below, reload extension, and look at extension console
  //console.log('I am a background script, about to throw an error');
  //throw new Error('Is this error caught?');
  </code></pre>