UNPKG

2.58 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 */
12/** Extracts StackFrames from the Error */
13export function parse(err) {
14 if (!err.stack) {
15 return [];
16 }
17 var lines = err.stack.split('\n').slice(1);
18 return lines
19 .map(function (line) {
20 if (line.match(/^\s*[-]{4,}$/)) {
21 return {
22 columnNumber: null,
23 fileName: line,
24 functionName: null,
25 lineNumber: null,
26 methodName: null,
27 native: null,
28 typeName: null,
29 };
30 }
31 var lineMatch = line.match(/at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/);
32 if (!lineMatch) {
33 return undefined;
34 }
35 var object = null;
36 var method = null;
37 var functionName = null;
38 var typeName = null;
39 var methodName = null;
40 var isNative = lineMatch[5] === 'native';
41 if (lineMatch[1]) {
42 functionName = lineMatch[1];
43 var methodStart = functionName.lastIndexOf('.');
44 if (functionName[methodStart - 1] === '.') {
45 // eslint-disable-next-line no-plusplus
46 methodStart--;
47 }
48 if (methodStart > 0) {
49 object = functionName.substr(0, methodStart);
50 method = functionName.substr(methodStart + 1);
51 var objectEnd = object.indexOf('.Module');
52 if (objectEnd > 0) {
53 functionName = functionName.substr(objectEnd + 1);
54 object = object.substr(0, objectEnd);
55 }
56 }
57 typeName = null;
58 }
59 if (method) {
60 typeName = object;
61 methodName = method;
62 }
63 if (method === '<anonymous>') {
64 methodName = null;
65 functionName = null;
66 }
67 var properties = {
68 columnNumber: parseInt(lineMatch[4], 10) || null,
69 fileName: lineMatch[2] || null,
70 functionName: functionName,
71 lineNumber: parseInt(lineMatch[3], 10) || null,
72 methodName: methodName,
73 native: isNative,
74 typeName: typeName,
75 };
76 return properties;
77 })
78 .filter(function (callSite) { return !!callSite; });
79}
80//# sourceMappingURL=stacktrace.js.map
\No newline at end of file