plogger-sdk
Version:
Typescript based frontend logging library compatible with multiple transports and JS/TS frameworks
78 lines • 3.64 kB
JavaScript
export function isError(e) {
return e instanceof Error;
}
export function stringifyErrorObject(e) {
var newValue = Object.getOwnPropertyNames(e).reduce(function (obj, propName) {
obj[propName] = e[propName];
return obj;
}, { name: e.name });
return newValue;
}
export function toErrorObject(error) {
var _a;
return {
nativeError: error,
name: (_a = error.name) !== null && _a !== void 0 ? _a : "Error",
message: error.message,
stack: _getErrorTrace(error),
};
}
function _getErrorTrace(error) {
var _a, _b, _c;
return (_c = (_b = (_a = error === null || error === void 0 ? void 0 : error.stack) === null || _a === void 0 ? void 0 : _a.split("\n")) === null || _b === void 0 ? void 0 : _b.filter(function (line) { return !line.includes("Error: "); })) === null || _c === void 0 ? void 0 : _c.reduce(function (result, line) {
var stackData = stackLineToStackFrame(line);
if (!Object.values(stackData).every(function (el) { return el === undefined; })) {
result.push(stackData);
}
return result;
}, []);
}
function stackLineToStackFrame(line) {
var pathRegexes = [
/^\s?((?:at)\s)?(?:(?<method>[\w.]*)(?:\s|@))?\(?(?:file|https|http|webpack|[^@]+)?(?:file:)?:\/?\/?\w*:?\d*\/(?<fileName>[\w./-]+)\??(?::(?<lineNumber>\d+))?(?::(?<colNumber>\d+))?/,
/(?:(?:file|https?|global code|[^@]+)@)?(?:file:)?(?<fileName>(?:\/[^:/]+){2,})(?::(?<lineNumber>\d+))?(?::(?<colNumber>\d+))?/,
];
var href = globalThis.location.origin;
var pathResult = {
fullFilePath: undefined,
fileName: undefined,
fileNameWithLine: undefined,
fileColumn: undefined,
fileLine: undefined,
filePath: undefined,
filePathWithLine: undefined,
method: undefined,
};
if (line != null) {
for (var i = 0; i < pathRegexes.length; i++) {
var pathRegex = pathRegexes[i];
var match = line.trim().match(pathRegex);
if (match) {
var match_group = match["groups"];
pathResult.filePath = match_group["fileName"].replace(/\?.*$/, "");
pathResult.fullFilePath = "".concat(href).concat(pathResult.filePath);
var pathParts = pathResult.filePath.split("/");
pathResult.fileName = pathParts[pathParts.length - 1];
pathResult.fileLine = match_group["lineNumber"];
pathResult.fileColumn = match_group["colNumber"];
pathResult.method = match_group["method"];
pathResult.filePathWithLine = "".concat(pathResult.filePath, ":").concat(pathResult.fileLine);
pathResult.fileNameWithLine = "".concat(pathResult.fileName, ":").concat(pathResult.fileLine);
break;
}
}
}
return pathResult;
}
export function getCallerStackFrame(stackDepthLevel, error) {
var _a, _b, _c;
if (error === void 0) { error = Error(); }
var userAgentString = (navigator === null || navigator === void 0 ? void 0 : navigator.userAgent) || "";
var _stackDepthLevel = userAgentString.match(/.*Firefox.*/)
? stackDepthLevel - 1
: stackDepthLevel;
return stackLineToStackFrame((_c = (_b = (_a = error === null || error === void 0 ? void 0 : error.stack) === null || _a === void 0 ? void 0 : _a.split("\n")) === null || _b === void 0 ? void 0 : _b.filter(function (line) {
return !line.includes("Error: ");
})) === null || _c === void 0 ? void 0 : _c[_stackDepthLevel]);
}
//# sourceMappingURL=errorObj.js.map