se-report
Version:
report js error to your log-server
114 lines (105 loc) • 3.21 kB
JavaScript
var _log_map = {};
module.exports = {
isOBJByType: function(o, type) {
return Object.prototype.toString.call(o) === "[object " + (type || "Object") + "]";
},
isOBJ: function(obj) {
var type = typeof obj;
return type === "object" && !!obj;
},
isEmpty: function(obj) {
if (obj === null) return true;
if (this.isOBJByType(obj, "Number")) {
return false;
}
return !obj;
},
extend: function(src, source) {
for (var key in source) {
src[key] = source[key];
}
return src;
},
GET: function(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
},
processError: function(errObj) {
try {
if (errObj.stack) {
var url = errObj.stack.match("https?://[^\n]+");
url = url ? url[0] : "";
var rowCols = url.match(":(\\d+):(\\d+)");
if (!rowCols) {
rowCols = [0, 0, 0];
}
var stack = this.processStackMsg(errObj);
return {
msg: stack,
rowNum: rowCols[1],
colNum: rowCols[2],
target: url.replace(rowCols[0], ""),
_orgMsg: errObj.toString()
};
} else {
//ie 独有 error 对象信息,try-catch 捕获到错误信息传过来,造成没有msg
if (errObj.name && errObj.message && errObj.description) {
return {
msg: JSON.stringify(errObj)
};
}
return errObj;
}
} catch (err) {
return errObj;
}
},
processStackMsg: function(error) {
var stack = error.stack
.replace(/\n/gi, "")
.split(/\bat\b/)
.slice(0, 9)
.join("@")
.replace(/\?[^:]+/gi, "");
var msg = error.toString();
if (stack.indexOf(msg) < 0) {
stack = msg + "@" + stack;
}
return stack;
},
isRepeat: function(error, repeat) {
if (!this.isOBJ(error)) return true;
var msg = error.msg;
var times = _log_map[msg] = (parseInt(_log_map[msg], 10) || 0) + 1;
return times > repeat;
},
_report_log_tostring: function(error, index) {
var param = [];
var params = [];
var stringify = [];
if (this.isOBJ(error)) {
error.level = error.level || _config.level;
for (var key in error) {
var value = error[key];
if (!this.isEmpty(value)) {
if (this.isOBJ(value)) {
try {
value = JSON.stringify(value);
} catch (err) {
value = "[BJ_REPORT detect value stringify error] " + err.toString();
}
}
stringify.push(key + ":" + value);
param.push(key + "=" + encodeURIComponent(value));
params.push(key + "[" + index + "]=" + encodeURIComponent(value));
}
}
}
// msg[0]=msg&target[0]=target -- combo report
// msg:msg,target:target -- ignore
// msg=msg&target=target -- report with out combo
return [params.join("&"), stringify.join(","), param.join("&")];
}
};