Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

调试error方法 #1

Open
ppya0812 opened this issue Jun 19, 2017 · 0 comments
Open

调试error方法 #1

ppya0812 opened this issue Jun 19, 2017 · 0 comments

Comments

@ppya0812
Copy link
Owner

window.onerror = function (msg, url, line, col, error) {
//没有URL不上报!上报也不知道错误
if (msg != "Script error." && !url) {
return true;
}
//脚本的异常数降低了10倍
setTimeout(function () {
var data = {};
//不一定所有浏览器都支持col参数
col = col || (window.event && window.event.errorCharacter) || 0;

  data.url = url;
  data.line = line;
  data.col = col;
  if (!!error && !!error.stack) {
   //如果浏览器有堆栈信息,直接使用
    data.msg = error.stack.toString();
  } else if (!!arguments.callee) {
    //尝试通过callee拿堆栈信息
    var ext = [];
    var f = arguments.callee.caller, c = 3;
    //这里只拿三层堆栈信息
    while (f && (--c > 0)) {
      ext.push(f.toString());
      if (f === f.caller) {
        break;//如果有环
      }
      f = f.caller;
    }
    ext = ext.join(",");
    data.msg = error.stack.toString();
  }
  //把data上报到后台!
  console.log(data);

}, 0);
return true;
};

@ppya0812 ppya0812 changed the title 调试error 调试error方法 Jan 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant