44

In a Chrome extension, is there a way to globally trap/handle Javascript errors occurring in a content-script? (e.g. for submission to a Javascript error tracking service like bugsnag)

Ideally, I would setup a global window.onerror 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.

I created a test extension to show this busted behaviour for content scripts. Details below. Interesting findings:

Reproducing busted window.onerror for Chrome extension content script.

In a new folder, create manifest.json, content-script.js and background-script.js. Then load into Chrome via Window > Extensions > Load unpacked Extension.

  • To see the busted window.onerror info for a content-script error, reload a webpage and look at the devtools console

manifest.json

{
    "name": "Chrome extension content-script errors test",
    "manifest_version": 2,
    "version": "0.0.1",
    "background": {
      "scripts": [ "background-script.js" ]
    },
    "content_scripts": [{
        "matches": ["<all_urls>"],
        "js": ["content-script.js"]
    }]
}

content-script.js

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?');

background-script.js

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?');
huczilla
  • 953
  • 1
  • 9
  • 11
  • 1
    Until that Chrome bug is fixed, here is a method to capture errors in the webpage context. You'll have to sort out if the error comes from the page or from your content script though. http://stackoverflow.com/a/20399910/1507998 – rsanchez Feb 05 '15 at 16:07
  • I already tried the approach described, and it's not working for me. window.onerror within the hosting webpage does indeed trap the error thrown from the extension content script. But the errorEvent supplied doesn't have any useful info either. I see: ` { "message": "Script error.", "filename": "", "lineno": 0, "colno": 0, "error": null } ` – huczilla Feb 05 '15 at 16:17
  • @huczilla Could you please edit out your "update" and post it as an answer? The question is in an unhealthy state of having no positive answers and yours is as close to correct as there is. – Xan Jul 03 '15 at 15:03
  • @Xan done as requested. – huczilla Jul 03 '15 at 21:27

3 Answers3

35

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!
huczilla
  • 953
  • 1
  • 9
  • 11
  • 1
    Enjoy your rep, well deserved. – Xan Jul 05 '15 at 23:28
  • Is this still an issue? I noticed the bug report is still open, but when I put a `window.onerror` at the top of my content script I get a full error response logged to the console. I get a message, line number, stack trace, everything that you expect – doublea Jan 30 '20 at 20:44
0

See the most complete errors handling by Chrome extension content script in https://github.com/barbushin/javascript-errors-notifier/blob/master/content.js - it's source code of JavaScript Errors Notifier extension.

barbushin
  • 5,165
  • 5
  • 37
  • 43
-1

Check out TraceKit, that's what we use at our company to do all front-end error logging. If you combine it with a javascript client library for your favorite logging service you're all set.

Wim Mostmans
  • 3,485
  • 1
  • 20
  • 20