获取页面上的所有javascript错误/ javascript错误处理 [英] Get all javascript errors on page/javascript error handling

查看:170
本文介绍了获取页面上的所有javascript错误/ javascript错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to be able to send myself all the javascript errors on a page. I am an extension developer, so the following has an emphasis on making sure the dom is ready before making calls to it.

我调查了向 throw 添加一些功能以发送或邮寄异常,但我没有找到这是可能的。

I investigated adding some functionality to the throw to also send or mail the exceptions, but I didn't find this to be possible.

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

1:main solution to this, a window.onerror handler:

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; 
}

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

May also need to do same thing via jQuery:

$(window).error( 
  function(e, url, line){
    //handle error
  }
);

这会导致您的代码停止执行。

The downfall of this is that your code has stopped executing.

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

To avoid errors stopping execution, it's best to use callbacks to make sure code executes in a certain sequence, and to utilize events wherever possible. You can also protect dom calls with a few techniques

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

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

You can also consider running code on 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
};

另一种技巧

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...');
}

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

Using these techniques and just debugging your application, you should be fairly well off. Since console.error, .warn, and .log statements cannot be retrieved and reported back to your, a small alternative suite is provided below:

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() );
}

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

As a last ditch, you can continually attempt to execute a chunk of code until it executes without errors. This is "2" below.

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

2:group code in large chunks and attempting to re-execute x seconds later after catching an error, or continue to next chunk of code

//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);
  }
}

然后使用它像

//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]);

#2摘要:在函数中对代码进行分组并在try-catch块中运行,如果出现错误则重复抓住了

#2 Summary: Group code in functions and run in try-catch block, repeating if an errors are caught

推荐答案

我有我的工作与window.onerror并没有停止我的javascript:

I have mine working with window.onerror and it doesn't stop my javascript:

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

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

Note that controller.sendLog is a function that sends this data to a logging php.

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

Can be because you're causing some javascript error inside that function?

这篇关于获取页面上的所有javascript错误/ javascript错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆