I noticed that rollbar items are not reported for errors that occur in callback in chrome.tabs calls. For example the following error will not get reported:
chrome.tabs.getCurrent(function(currentTab) {
throw "This is critical";
});
If the throw
statement is outside of the chrome.tabs callback, it gets reported to rollbar as expected.
The behavior is the same regardless if it happens in a background script, or a extension page accessed through a chrome-extension://
url.
Is there a solution that will allow rollbar to track errors in these callbacks?
Errors thrown in chrome API callbacks, do not seem to be caught by the onerror
listener. Here is a related discussion from the chromium project: https://bugs.chromium.org/p/chromium/issues/detail?id=357568
Chrome itself behaves strangely in the tabs callbacks, even when not using Rollbar.
Here is the test code I used:
chrome.tabs.getSelected(null, function(tab){
console.log('tabs callback', window);
throw 'Error in tab';
});
setTimeout(function(){
console.log('timeout callback', window);
throw 'Error in timeout';
}, 100);
For the timeout callback, both the background console and the Errors view for the extension show the correct code location for the exception.
However, for the tabs callback the location is shown as _generated_background_page.html with no backtrace or line numbers, both in the console and in the Errors view. Chrome itself doesn't seem to know more about where the error came from.
The window object in both callbacks does have the onerror
hook set correctly to Rollbar's handler. However in the tabs example, it never gets called. Chrome appears to be catching the error before it gets to the onerror
handler, or whatever kind of thread this is doesn't even use the onerror
handler.
As a workaround, I tried wrapping the code in a try/catch, and reporting to Rollbar:
chrome.tabs.getSelected(null, function(tab){
try {
console.log('tabs callback', window);
throw 'Error in tab';
}
catch (e) {
console.log('error', e);
Rollbar.log(e);
}
});
This works, but the error object still reports its location as _generated_background_page.html. At least it does have the correct error message.