Rollbar无法在chrome.tabs回调中检测到错误

时间:2019-04-05 10:03:36

标签: google-chrome-extension rollbar

我注意到,针对chrome.tabs调用中的回调发生的错误,并未报告滚动条项目。例如,将不会报告以下错误:

chrome.tabs.getCurrent(function(currentTab) {
    throw "This is critical";
});

如果throw语句不在chrome.tabs回调中,则会按预期将其报告给rollbar。

无论是在后台脚本中还是通过chrome-extension://网址访问扩展页面时,行为都是相同的。

有没有一种解决方案可以让rollbar跟踪这些回调中的错误?

Chrome onerror侦听器似乎未捕获chrome API回调中引发的错误。以下是铬项目的相关讨论:https://bugs.chromium.org/p/chromium/issues/detail?id=357568

1 个答案:

答案 0 :(得分:1)

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.