UNPKG

2.53 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 fron 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 methodStart--;
46 }
47 if (methodStart > 0) {
48 object = functionName.substr(0, methodStart);
49 method = functionName.substr(methodStart + 1);
50 var objectEnd = object.indexOf('.Module');
51 if (objectEnd > 0) {
52 functionName = functionName.substr(objectEnd + 1);
53 object = object.substr(0, objectEnd);
54 }
55 }
56 typeName = null;
57 }
58 if (method) {
59 typeName = object;
60 methodName = method;
61 }
62 if (method === '<anonymous>') {
63 methodName = null;
64 functionName = null;
65 }
66 var properties = {
67 columnNumber: parseInt(lineMatch[4], 10) || null,
68 fileName: lineMatch[2] || null,
69 functionName: functionName,
70 lineNumber: parseInt(lineMatch[3], 10) || null,
71 methodName: methodName,
72 native: isNative,
73 typeName: typeName,
74 };
75 return properties;
76 })
77 .filter(function (callSite) { return !!callSite; });
78}
79//# sourceMappingURL=stacktrace.js.map
\No newline at end of file