1 | import { addContextToFrame, basename, dirname, SyncPromise } from '@sentry/utils';
|
2 | import { readFile } from 'fs';
|
3 | import { LRUMap } from 'lru_map';
|
4 | import * as stacktrace from './stacktrace';
|
5 |
|
6 | var DEFAULT_LINES_OF_CONTEXT = 7;
|
7 | var FILE_CONTENT_CACHE = new LRUMap(100);
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export function resetFileContentCache() {
|
13 | FILE_CONTENT_CACHE.clear();
|
14 | }
|
15 |
|
16 | function getFunction(frame) {
|
17 | try {
|
18 | return frame.functionName || frame.typeName + "." + (frame.methodName || '<anonymous>');
|
19 | }
|
20 | catch (e) {
|
21 |
|
22 |
|
23 |
|
24 | return '<anonymous>';
|
25 | }
|
26 | }
|
27 | var mainModule = ((require.main && require.main.filename && dirname(require.main.filename)) ||
|
28 | global.process.cwd()) + "/";
|
29 |
|
30 | function getModule(filename, base) {
|
31 | if (!base) {
|
32 | base = mainModule;
|
33 | }
|
34 |
|
35 | var file = basename(filename, '.js');
|
36 | filename = dirname(filename);
|
37 | var n = filename.lastIndexOf('/node_modules/');
|
38 | if (n > -1) {
|
39 |
|
40 | return filename.substr(n + 14).replace(/\//g, '.') + ":" + file;
|
41 | }
|
42 |
|
43 |
|
44 | n = (filename + "/").lastIndexOf(base, 0);
|
45 | if (n === 0) {
|
46 | var moduleName = filename.substr(base.length).replace(/\//g, '.');
|
47 | if (moduleName) {
|
48 | moduleName += ':';
|
49 | }
|
50 | moduleName += file;
|
51 | return moduleName;
|
52 | }
|
53 | return file;
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function readSourceFiles(filenames) {
|
62 |
|
63 | if (filenames.length === 0) {
|
64 | return SyncPromise.resolve({});
|
65 | }
|
66 | return new SyncPromise(function (resolve) {
|
67 | var sourceFiles = {};
|
68 | var count = 0;
|
69 | var _loop_1 = function (i) {
|
70 | var filename = filenames[i];
|
71 | var cache = FILE_CONTENT_CACHE.get(filename);
|
72 |
|
73 | if (cache !== undefined) {
|
74 |
|
75 |
|
76 | if (cache !== null) {
|
77 | sourceFiles[filename] = cache;
|
78 | }
|
79 | count++;
|
80 |
|
81 |
|
82 | if (count === filenames.length) {
|
83 | resolve(sourceFiles);
|
84 | }
|
85 | return "continue";
|
86 | }
|
87 | readFile(filename, function (err, data) {
|
88 | var content = err ? null : data.toString();
|
89 | sourceFiles[filename] = content;
|
90 |
|
91 |
|
92 | FILE_CONTENT_CACHE.set(filename, content);
|
93 | count++;
|
94 | if (count === filenames.length) {
|
95 | resolve(sourceFiles);
|
96 | }
|
97 | });
|
98 | };
|
99 |
|
100 | for (var i = 0; i < filenames.length; i++) {
|
101 | _loop_1(i);
|
102 | }
|
103 | });
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 | export function extractStackFromError(error) {
|
109 | var stack = stacktrace.parse(error);
|
110 | if (!stack) {
|
111 | return [];
|
112 | }
|
113 | return stack;
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 | export function parseStack(stack, options) {
|
119 | var filesToRead = [];
|
120 | var linesOfContext = options && options.frameContextLines !== undefined ? options.frameContextLines : DEFAULT_LINES_OF_CONTEXT;
|
121 | var frames = stack.map(function (frame) {
|
122 | var parsedFrame = {
|
123 | colno: frame.columnNumber,
|
124 | filename: frame.fileName || '',
|
125 | function: getFunction(frame),
|
126 | lineno: frame.lineNumber,
|
127 | };
|
128 | var isInternal = frame.native ||
|
129 | (parsedFrame.filename &&
|
130 | !parsedFrame.filename.startsWith('/') &&
|
131 | !parsedFrame.filename.startsWith('.') &&
|
132 | parsedFrame.filename.indexOf(':\\') !== 1);
|
133 | // in_app is all that's not an internal Node function or a module within node_modules
|
134 | // note that isNative appears to return true even for node core libraries
|
135 | // see https://github.com/getsentry/raven-node/issues/176
|
136 | parsedFrame.in_app =
|
137 | !isInternal && parsedFrame.filename !== undefined && parsedFrame.filename.indexOf('node_modules/') === -1;
|
138 | // Extract a module name based on the filename
|
139 | if (parsedFrame.filename) {
|
140 | parsedFrame.module = getModule(parsedFrame.filename);
|
141 | if (!isInternal && linesOfContext > 0) {
|
142 | filesToRead.push(parsedFrame.filename);
|
143 | }
|
144 | }
|
145 | return parsedFrame;
|
146 | });
|
147 |
|
148 | if (linesOfContext <= 0) {
|
149 | return SyncPromise.resolve(frames);
|
150 | }
|
151 | try {
|
152 | return addPrePostContext(filesToRead, frames, linesOfContext);
|
153 | }
|
154 | catch (_) {
|
155 |
|
156 |
|
157 | return SyncPromise.resolve(frames);
|
158 | }
|
159 | }
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | function addPrePostContext(filesToRead, frames, linesOfContext) {
|
167 | return new SyncPromise(function (resolve) {
|
168 | return readSourceFiles(filesToRead).then(function (sourceFiles) {
|
169 | var result = frames.map(function (frame) {
|
170 | if (frame.filename && sourceFiles[frame.filename]) {
|
171 | try {
|
172 | var lines = sourceFiles[frame.filename].split('\n');
|
173 | addContextToFrame(lines, frame, linesOfContext);
|
174 | }
|
175 | catch (e) {
|
176 |
|
177 |
|
178 | }
|
179 | }
|
180 | return frame;
|
181 | });
|
182 | resolve(result);
|
183 | });
|
184 | });
|
185 | }
|
186 |
|
187 |
|
188 |
|
189 | export function getExceptionFromError(error, options) {
|
190 | var name = error.name || error.constructor.name;
|
191 | var stack = extractStackFromError(error);
|
192 | return new SyncPromise(function (resolve) {
|
193 | return parseStack(stack, options).then(function (frames) {
|
194 | var result = {
|
195 | stacktrace: {
|
196 | frames: prepareFramesForEvent(frames),
|
197 | },
|
198 | type: name,
|
199 | value: error.message,
|
200 | };
|
201 | resolve(result);
|
202 | });
|
203 | });
|
204 | }
|
205 |
|
206 |
|
207 |
|
208 | export function parseError(error, options) {
|
209 | return new SyncPromise(function (resolve) {
|
210 | return getExceptionFromError(error, options).then(function (exception) {
|
211 | resolve({
|
212 | exception: {
|
213 | values: [exception],
|
214 | },
|
215 | });
|
216 | });
|
217 | });
|
218 | }
|
219 |
|
220 |
|
221 |
|
222 | export function prepareFramesForEvent(stack) {
|
223 | if (!stack || !stack.length) {
|
224 | return [];
|
225 | }
|
226 | var localStack = stack;
|
227 | var firstFrameFunction = localStack[0].function || '';
|
228 | if (firstFrameFunction.indexOf('captureMessage') !== -1 || firstFrameFunction.indexOf('captureException') !== -1) {
|
229 | localStack = localStack.slice(1);
|
230 | }
|
231 |
|
232 | return localStack.reverse();
|
233 | }
|
234 |
|
\ | No newline at end of file |