apminsight
Version:
monitor nodejs applications
70 lines (60 loc) • 1.97 kB
JavaScript
var constants = require("./../constants");
var utils = require("./../util/utils");
function ErrorInfo(error) {
this._time = new Date().getTime();
this._error = error;
this._stackFrames = null;
}
ErrorInfo.prototype.getTime = function () {
return this._time;
};
ErrorInfo.prototype.getType = function () {
if (this._errorType) {
return this._errorType;
}
var defaultErrType = "Error";
var errorCode = this._error.code;
if (
!utils.isEmpty(this._error.name) &&
this._error.name !== defaultErrType
) {
this._errorType = this._error.name;
} else if (!utils.isEmpty(errorCode)) {
this._errorType =
typeof errorCode === "string" ? errorCode : errorCode.toString();
} else {
this._errorType = defaultErrType;
}
return this._errorType;
};
ErrorInfo.prototype.getLevel = function () {
return "FATAL";
};
ErrorInfo.prototype.getMessage = function () {
return this._error.message;
};
ErrorInfo.prototype.getErrorStackFrames = function () {
if (this._stackFrames) {
return this._stackFrames;
}
var filtered = this._error.stack.split("\n").filter(function (line) {
return line.match(constants.stackTraceRegex);
});
this._stackFrames = filtered.map(function (line) {
var tokens = line.replace(/^\s+/, "").split(/\s+/).slice(1);
var locationParts = extractLocation(tokens.pop());
var functionName = tokens.join(" ") || "";
var fileName = locationParts[0] ? locationParts[0] : "";
return ["", functionName, fileName, locationParts[1]];
});
return this._stackFrames;
};
function extractLocation(urlLike) {
if (urlLike.indexOf(":") === -1) {
return [urlLike];
}
var regExp = /(.+?)(?:(\d+))?(?::(\d+))?$/;
var parts = regExp.exec(urlLike.replace(/[()]/g, ""));
return [parts[1], parts[2] || "", parts[3] || ""];
}
module.exports = ErrorInfo;