我想知道是否有人有全局处理JavaScript错误并将它们从客户端浏览器发送到服务器的经验。
我想我的观点很清楚,我想知道客户端发生的每一个异常、错误、编译错误等,并将它们发送给服务器报告它们。
我主要使用MooTools和head.js(用于JS端)和Django用于服务器端。
head.js
我会看看window.onerror
例子:
window.onerror = function(message, url, lineNumber) { //save error and send to server for example. return true; };
请记住,返回true将阻止触发默认处理程序,返回false将让默认处理程序运行。
如果你的网站使用谷歌分析,你可以做我做的:
window.onerror = function(message, source, lineno, colno, error) { if (error) message = error.stack; ga('send', 'event', 'window.onerror', message, navigator.userAgent); }
上面代码的一些注释:
代码就绪后,你可以这样查看用户的Javascript错误:
Behavior
Top Events
window.onerror
Secondary dimension
Event Label
不要尝试使用第三方服务,而是尝试自己的服务。
错误处理程序可以捕获以下场景:
捕捉Javascript错误:
window.addEventListener('error', function (e) { //It Will handle JS errors })
捕获AngularJS错误:
app.config(function ($provide) { $provide.decorator('$exceptionHandler', function ($delegate) { return function (exception, cause) { //It will handle AngualarJS errors } }) })