Catching background error

219 views
Skip to first unread message

CrmDev

unread,
Sep 1, 2013, 2:32:33 PM9/1/13
to chromium-...@chromium.org
Hi,

I'm looking for a way to catch js errors on my background page. For starters I would like to print in the console my message every time there is an error with my bg script.

I'm playing a little with window.onerror, tried this:

window.onerror = function(message, url, lineNumber) {  
  console.log(message + ' - ' +  url + ' - ' + lineNumber);
  return true;
}; 

However, some errors still show without being printed... 

Any idea why? And what is the best solution for catching those errors?

Thanks



Rob Wu

unread,
Sep 3, 2013, 1:19:09 PM9/3/13
to chromium-...@chromium.org
Why do you want to catch and print the errors yourself? This does already happen by default. Just open the console of the background page.

To answer your original questiom: The Chrome extension API also logs errors with "console.error" instead of "throw new Error( ... )". If you wish to intercept these errors, you have to override the console.error method. By doing this, the line number next to the error is lost though. If you need to know this value, parse the value of new Error().stack.

---
Rob
https://robwu.nl/
Reply all
Reply to author
Forward
0 new messages