|
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 |
| - |
13 |
| -/** Decoded StackFrame */ |
14 |
| -export interface StackFrame { |
15 |
| - fileName: string; |
16 |
| - lineNumber: number; |
17 |
| - functionName: string; |
18 |
| - typeName: string; |
19 |
| - methodName: string; |
20 |
| - native: boolean; |
21 |
| - columnNumber: number; |
22 |
| -} |
23 |
| - |
24 |
| -/** Extracts StackFrames fron the Error */ |
25 |
| -export function parse(err: Error): StackFrame[] { |
26 |
| - if (!err.stack) { |
27 |
| - return []; |
28 |
| - } |
29 |
| - |
30 |
| - const lines = err.stack.split('\n').slice(1); |
31 |
| - |
32 |
| - return lines |
33 |
| - .map(line => { |
34 |
| - if (line.match(/^\s*[-]{4,}$/)) { |
35 |
| - return { |
36 |
| - columnNumber: null, |
37 |
| - fileName: line, |
38 |
| - functionName: null, |
39 |
| - lineNumber: null, |
40 |
| - methodName: null, |
41 |
| - native: null, |
42 |
| - typeName: null, |
43 |
| - }; |
44 |
| - } |
45 |
| - |
46 |
| - const lineMatch = line.match(/at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/); |
47 |
| - if (!lineMatch) { |
48 |
| - return undefined; |
49 |
| - } |
50 |
| - |
51 |
| - let object = null; |
52 |
| - let method = null; |
53 |
| - let functionName = null; |
54 |
| - let typeName = null; |
55 |
| - let methodName = null; |
56 |
| - const isNative = lineMatch[5] === 'native'; |
57 |
| - |
58 |
| - if (lineMatch[1]) { |
59 |
| - functionName = lineMatch[1]; |
60 |
| - let methodStart = functionName.lastIndexOf('.'); |
61 |
| - if (functionName[methodStart - 1] === '.') { |
62 |
| - methodStart--; |
63 |
| - } |
64 |
| - if (methodStart > 0) { |
65 |
| - object = functionName.substr(0, methodStart); |
66 |
| - method = functionName.substr(methodStart + 1); |
67 |
| - const objectEnd = object.indexOf('.Module'); |
68 |
| - if (objectEnd > 0) { |
69 |
| - functionName = functionName.substr(objectEnd + 1); |
70 |
| - object = object.substr(0, objectEnd); |
71 |
| - } |
72 |
| - } |
73 |
| - typeName = null; |
74 |
| - } |
75 |
| - |
76 |
| - if (method) { |
77 |
| - typeName = object; |
78 |
| - methodName = method; |
79 |
| - } |
80 |
| - |
81 |
| - if (method === '<anonymous>') { |
82 |
| - methodName = null; |
83 |
| - functionName = null; |
84 |
| - } |
85 |
| - |
86 |
| - const properties = { |
87 |
| - columnNumber: parseInt(lineMatch[4], 10) || null, |
88 |
| - fileName: lineMatch[2] || null, |
89 |
| - functionName, |
90 |
| - lineNumber: parseInt(lineMatch[3], 10) || null, |
91 |
| - methodName, |
92 |
| - native: isNative, |
93 |
| - typeName, |
94 |
| - }; |
95 |
| - |
96 |
| - return properties; |
97 |
| - }) |
98 |
| - .filter(callSite => !!callSite) as StackFrame[]; |
99 |
| -} |
| 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 | + |
| 13 | +/** Decoded StackFrame */ |
| 14 | +export interface StackFrame { |
| 15 | + fileName: string; |
| 16 | + lineNumber: number; |
| 17 | + functionName: string; |
| 18 | + typeName: string; |
| 19 | + methodName: string; |
| 20 | + native: boolean; |
| 21 | + columnNumber: number; |
| 22 | +} |
| 23 | + |
| 24 | +/** Extracts StackFrames fron the Error */ |
| 25 | +export function parse(err: Error): StackFrame[] { |
| 26 | + if (!err.stack) { |
| 27 | + return []; |
| 28 | + } |
| 29 | + |
| 30 | + const lines = err.stack.split('\n').slice(1); |
| 31 | + |
| 32 | + return lines |
| 33 | + .map(line => { |
| 34 | + if (line.match(/^\s*[-]{4,}$/)) { |
| 35 | + return { |
| 36 | + columnNumber: null, |
| 37 | + fileName: line, |
| 38 | + functionName: null, |
| 39 | + lineNumber: null, |
| 40 | + methodName: null, |
| 41 | + native: null, |
| 42 | + typeName: null, |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + const lineMatch = line.match(/at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/); |
| 47 | + if (!lineMatch) { |
| 48 | + return undefined; |
| 49 | + } |
| 50 | + |
| 51 | + let object = null; |
| 52 | + let method = null; |
| 53 | + let functionName = null; |
| 54 | + let typeName = null; |
| 55 | + let methodName = null; |
| 56 | + const isNative = lineMatch[5] === 'native'; |
| 57 | + |
| 58 | + if (lineMatch[1]) { |
| 59 | + functionName = lineMatch[1]; |
| 60 | + let methodStart = functionName.lastIndexOf('.'); |
| 61 | + if (functionName[methodStart - 1] === '.') { |
| 62 | + methodStart--; |
| 63 | + } |
| 64 | + if (methodStart > 0) { |
| 65 | + object = functionName.substr(0, methodStart); |
| 66 | + method = functionName.substr(methodStart + 1); |
| 67 | + const objectEnd = object.indexOf('.Module'); |
| 68 | + if (objectEnd > 0) { |
| 69 | + functionName = functionName.substr(objectEnd + 1); |
| 70 | + object = object.substr(0, objectEnd); |
| 71 | + } |
| 72 | + } |
| 73 | + typeName = null; |
| 74 | + } |
| 75 | + |
| 76 | + if (method) { |
| 77 | + typeName = object; |
| 78 | + methodName = method; |
| 79 | + } |
| 80 | + |
| 81 | + if (method === '<anonymous>') { |
| 82 | + methodName = null; |
| 83 | + functionName = null; |
| 84 | + } |
| 85 | + |
| 86 | + const properties = { |
| 87 | + columnNumber: parseInt(lineMatch[4], 10) || null, |
| 88 | + fileName: lineMatch[2] || null, |
| 89 | + functionName, |
| 90 | + lineNumber: parseInt(lineMatch[3], 10) || null, |
| 91 | + methodName, |
| 92 | + native: isNative, |
| 93 | + typeName, |
| 94 | + }; |
| 95 | + |
| 96 | + return properties; |
| 97 | + }) |
| 98 | + .filter(callSite => !!callSite) as StackFrame[]; |
| 99 | +} |
0 commit comments