2016-06-09 122 views
8

我发现https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/onerror它说:服务人员如何处理全局错误处理?

The onerror property of the ServiceWorkerContainer interface is an event handler fired whenever an error event occurs in the associated service workers.

但是我不能在Chrome(V51)得到这个工作。在主应用程序的范围,我跑从控制台下面的代码:

navigator.serviceWorker.onerror = function(e) { console.log('some identifiable string' + e); }; 

然后主动服务工作者的我引发的任意错误的范围之内:

f(); // f is undefined 

其结果是通常“未捕获的ReferenceError:f未定义(...)”错误消息,但未通过全局onerror处理程序记录。

MDN页面显示,自从v40版本开始,Chrome已经支持该API,但navigator.serviceWorker.onerror最初未定义,这导致我认为它未实现。有人熟悉这个吗?

+0

“*最初不确定*” - 你的意思是它确实有undefined'的'的值(这是原因能够),还是该财产根本不存在? – Bergi

+0

@Bergi它有一个'undefined'的值,这让我觉得不寻常,因为window.onerror最初是'null'。编辑:我加倍检查,这也是属性不存在''onerror'navigator.serviceWorker // false' –

回答

6

也许你试着设置onerror处理程序navigator.serviceWorker容器这样的:

// no effect outside service worker script 
navigator.serviceWorker.onerror = function() {...}; 

错误处理程序必须从服务工作者脚本内设置有self.onerrorself是一个特殊的变量/属性这里指的是ServiceWorkerGlobalScope)。 onerror回调仅提供一条错误消息。

// inside service worker script 
self.onerror = function(message) { 
    console.log(message); 
}; 

或者,你可以听服务人员的error事件,其中包括包含错误的位置的ErrorEvent

// inside service worker script 
self.addEventListener('error', function(e) { 
    console.log(e.filename, e.lineno, e.colno, e.message); 
}); 

这里有一个demo。务必从DevTools>资源>服务工作者(左面板)删除服务工作者因为这将填补这些失败的服务工作者注册:

enter image description here

我已经验证了以下浏览器支持onerror服务人员的实例中:

  • 铬51(稳定)和53(金丝雀)
  • 火狐47
  • 歌剧38(稳定)和39(开发者)

UPDATE

So when MDN describes the ServiceWorkerContainer interface, that is referring to self (ServiceWorkerGlobalScope) and not navigator.serviceWorker ?

我认为这是只为onerror属性为真(也许对其他事件有作为),我猜测该规范尚未更新,以反映商定的实施情况......

的服务人员工作组已决定从ServiceWorkerContainer到服务工人实例移动onerror,在GitHub的讨论(slightlyoff/ServiceWorker #198):

kinu commented on Apr 2, 2014

sgtm2. For error reporting (onerror stuff) we could probably do similar? E.g. moving .onerror handler from container to SW object, so that doc can explicitly know which SW the error is coming from (though it may need to attach handlers to multiple SWs).

然后有后续评论在相关指示缺乏有用的为onerror容器上的问题(slightlyoff/ServiceWorker #104):

jakearchibald commented on Apr 3, 2014

Thinking about the use-cases (following from #198)…

navigator.serviceWorker.onerror or navigator.serviceWorker.pending.onerror (whichever it becomes) are not useful for logging errors back to the server, as errors can happen outside of the life of any page. onerror inside the worker itself is best for that.

.pending.onerror is useful if you're updating the UI in response to an update. So maybe it's better as a statechange , although you'd need somewhere to put the error message.

That leaves errors that happen before the SW instance is created. AppCache has an error event that covers network-related update failures, and also parse failures. However, once again we'd lose any errors that happened outside the life of a page.

+0

感谢您的答案。所以当MDN描述ServiceWorkerContainer接口时,它指的是'self'('ServiceWorkerGlobalScope')而不是'navigator.serviceWorker'? –

+0

没问题。我不确定情况是否如此。对于'onerror'事件处理程序恰好是这样(也可能是其他事件)。根据我在2014年发现的GitHub讨论,我相信事件实际上已经移动,但文档没有更新。为了清楚起见,我会更新我的答案。 – tony19

+0

非常有趣。在这种情况下,这不仅仅是文档没有更新,但规范本身是不正确的:https://www.w3.org/TR/service-workers/#service-worker-container-event-handlers –