获取页面上的所有javascript错误/ javascript错误处理

Dev*_*ode 23 javascript

我希望能够在页面上发送自己的所有javascript错误.我是一个扩展开发人员,因此以下重点是在调用dom之前确保dom已准备就绪.

我调查了添加一些功能,throw也发送或邮寄异常,但我没有发现这是可能的.

1:主要解决方案,window.onerror处理程序:

window.onerror = function(e, url, line){
  mailError('onerror: ' + e + ' URL:' + url + ' Line:' + line);
  console.error('\nLine ' + line + ':');
  setTimeout(function(){retry();}, 100); //mainly useful in content scripts for extensions, 
  return true; 
}
Run Code Online (Sandbox Code Playgroud)

也可能需要通过jQuery做同样的事情:

$(window).error( 
  function(e, url, line){
    //handle error
  }
);
Run Code Online (Sandbox Code Playgroud)

这种情况的缺点是您的代码已停止执行.

为了避免错误停止执行,最好使用回调来确保代码按特定顺序执行,并尽可能利用事件.您还可以使用一些技巧保护dom通话

$(document).ready({
  //you usually won't get bad dom calls wrapping your code in this
});
Run Code Online (Sandbox Code Playgroud)

您还可以考虑在window.onload上运行代码

window.onload = function(){
  //you page will probably twitch upon executing code at this time
  //but you will almost never have a bad dom call
};
Run Code Online (Sandbox Code Playgroud)

另一种技术

if (document.getElementById('iNeedThisElement')) {
  //doin work!
  document.getElementById('iNeedThisElement').style.display = 'block';
} else {
  var stillNeedToTakeCareOfX = true; //false otherwise since it's undefined
  mailError('iNeedThisElement was unavailable...');
}
Run Code Online (Sandbox Code Playgroud)

使用这些技术并只调试您的应用程序,您应该相当富裕.由于无法检索console.error,.warn和.log语句并将其报告给您,因此下面提供了一个小型替代套件:

var Xe = { }; //Extreme error suite

function extraInfo(e){
   //e optional
   if(!e)e = true;

   //add an extra debug info, such as navigator or current URL
   return ' currentURL: '+ document.URL + 
        '\n userAgent: ' + navigator.userAgent + 
        '\n platform: '  + navigator.platform + 
        '\n userid: '    + localStorage.userid + 
        '\n language: '  + navigator.langauge + 
        '\n cookies?: '  + navigator.cookiesEnabled;
}

Xe.error = function(e){
  console.error(e); //maintain original functionality
  mailError('Xe err: ' + e + extraInfo() );
}


Xe.warn = function(e){
  console.warn(e);
  mailError('Xe warn: ' + e + extraInfo() );
}


Xe.log = function(e){
  console.log(e);
  mailError('Xe log: ' + e + extraInfo() );
}
Run Code Online (Sandbox Code Playgroud)

作为最后一个沟槽,您可以不断尝试执行一大块代码,直到它执行没有错误.这是下面的"2".

2:在大块中组合代码并在捕获错误后尝试重新执行x秒,或者继续下一代代码块

//defExe = DEFinitely EXEcute this code
  //functionArg, reference to a function holding a large chunk of code
  //seconds, timeout in milliseconds to re-attempt error free execution of this function
function defExe(functionArg, seconds) {
  //seconds is optional
  if (!seconds)seconds = 300;

  try {
    functionArg();
  } catch(e) {
    //mail the error plus extra info
    mailError('caught ' + e + ' attempting to re-execute ' + functionArg.name + ' in ' + seconds + ' milliseconds');

    //re-attempt to execute this code
    setTimeout(function(){ 
       defExe(functionArg, seconds); 
    }, seconds);
  }
}
Run Code Online (Sandbox Code Playgroud)

这就像它一样使用

//array to group function chunks
var fn = [ ];

fn[1] = function (){
  //a large chunk of javascript
}
defExe(fn[1]);

fn[2] = function(){
  //another chunk of your program
}
defExe(fn[2]);
Run Code Online (Sandbox Code Playgroud)

#2摘要:在函数中对代码进行分组并在try-catch块中运行,如果捕获到错误则重复

Ser*_*món 44

我有我的window.onerror工作,它不会停止我的JavaScript:

window.onerror = function(error, url, line) {
    controller.sendLog({acc:'error', data:'ERR:'+error+' URL:'+url+' L:'+line});
};
Run Code Online (Sandbox Code Playgroud)

请注意,controller.sendLog是一个将此数据发送到日志记录php的函数.

可能是因为你在该函数中导致了一些javascript错误?

  • 好吧,它会停止执行当前线程,因为它遇到错误,这是javascript中的正常行为.在我正在使用它的页面中,我有很多对象和事件在这里和那里触发,所以如果一个中断,那一个停止执行但是一个新事件触发相同的代码再次工作.我认为你不能避免它停止,唯一的方法是使用大量的try-catch块,而且,在try-catch块中行为是相同的,它停止执行块并跳转到赶上句子. (4认同)
  • 这仍然是捕获页面上所有错误的最佳方法吗?您还可以使用`window.addEventListener("error",listener)`吗? (2认同)