UNPKG

2.66 kBJavaScriptView Raw
1/**
2 * stack-trace - Parses node.js stack traces
3 *
4 * This was originally forked to fix this issue:
5 * https://github.com/felixge/node-stack-trace/issues/31
6 *
7 * Mar 19,2019 - #4fd379e
8 *
9 * https://github.com/felixge/node-stack-trace/
10 * @license MIT
11 */
12Object.defineProperty(exports, "__esModule", { value: true });
13/** Extracts StackFrames from the Error */
14function parse(err) {
15 if (!err.stack) {
16 return [];
17 }
18 var lines = err.stack.split('\n').slice(1);
19 return lines
20 .map(function (line) {
21 if (line.match(/^\s*[-]{4,}$/)) {
22 return {
23 columnNumber: null,
24 fileName: line,
25 functionName: null,
26 lineNumber: null,
27 methodName: null,
28 native: null,
29 typeName: null,
30 };
31 }
32 var lineMatch = line.match(/at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/);
33 if (!lineMatch) {
34 return undefined;
35 }
36 var object = null;
37 var method = null;
38 var functionName = null;
39 var typeName = null;
40 var methodName = null;
41 var isNative = lineMatch[5] === 'native';
42 if (lineMatch[1]) {
43 functionName = lineMatch[1];
44 var methodStart = functionName.lastIndexOf('.');
45 if (functionName[methodStart - 1] === '.') {
46 // eslint-disable-next-line no-plusplus
47 methodStart--;
48 }
49 if (methodStart > 0) {
50 object = functionName.substr(0, methodStart);
51 method = functionName.substr(methodStart + 1);
52 var objectEnd = object.indexOf('.Module');
53 if (objectEnd > 0) {
54 functionName = functionName.substr(objectEnd + 1);
55 object = object.substr(0, objectEnd);
56 }
57 }
58 typeName = null;
59 }
60 if (method) {
61 typeName = object;
62 methodName = method;
63 }
64 if (method === '<anonymous>') {
65 methodName = null;
66 functionName = null;
67 }
68 var properties = {
69 columnNumber: parseInt(lineMatch[4], 10) || null,
70 fileName: lineMatch[2] || null,
71 functionName: functionName,
72 lineNumber: parseInt(lineMatch[3], 10) || null,
73 methodName: methodName,
74 native: isNative,
75 typeName: typeName,
76 };
77 return properties;
78 })
79 .filter(function (callSite) { return !!callSite; });
80}
81exports.parse = parse;
82//# sourceMappingURL=stacktrace.js.map
\No newline at end of file