-
Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathparseStackTrace.js
More file actions
366 lines (349 loc) · 11.7 KB
/
parseStackTrace.js
File metadata and controls
366 lines (349 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {ReactStackTrace, ReactFunctionLocation} from 'shared/ReactTypes';
function parseStackTraceFromChromeStack(
stack: string,
skipFrames: number,
): ReactStackTrace {
if (stack.startsWith('Error: react-stack-top-frame\n')) {
// V8's default formatting prefixes with the error message which we
// don't want/need.
stack = stack.slice(29);
}
let idx = stack.indexOf('react_stack_bottom_frame');
if (idx === -1) {
idx = stack.indexOf('react-stack-bottom-frame');
}
if (idx !== -1) {
idx = stack.lastIndexOf('\n', idx);
}
if (idx !== -1) {
// Cut off everything after the bottom frame since it'll be internals.
stack = stack.slice(0, idx);
}
const frames = stack.split('\n');
const parsedFrames: ReactStackTrace = [];
// We skip top frames here since they may or may not be parseable but we
// want to skip the same number of frames regardless. I.e. we can't do it
// in the caller.
for (let i = skipFrames; i < frames.length; i++) {
const parsed = chromeFrameRegExp.exec(frames[i]);
if (!parsed) {
continue;
}
let name = parsed[1] || '';
let isAsync = parsed[8] === 'async ';
if (name === '<anonymous>') {
name = '';
} else if (name.startsWith('async ')) {
name = name.slice(5);
isAsync = true;
}
let filename = parsed[2] || parsed[5] || '';
if (filename === '<anonymous>') {
filename = '';
}
const line = +(parsed[3] || parsed[6] || 0);
const col = +(parsed[4] || parsed[7] || 0);
parsedFrames.push([name, filename, line, col, 0, 0, isAsync]);
}
return parsedFrames;
}
const firefoxFrameRegExp = /^([^@"]*(?:"[^"]*"[^@"]*)*)@(.+):(\d+):(\d+)$/;
function parseStackTraceFromFirefoxStack(
stack: string,
skipFrames: number,
): ReactStackTrace {
let idx = stack.indexOf('react_stack_bottom_frame');
if (idx === -1) {
idx = stack.indexOf('react-stack-bottom-frame');
}
if (idx !== -1) {
idx = stack.lastIndexOf('\n', idx);
}
if (idx !== -1) {
// Cut off everything after the bottom frame since it'll be internals.
stack = stack.slice(0, idx);
}
const frames = stack.split('\n');
const parsedFrames: ReactStackTrace = [];
// We skip top frames here since they may or may not be parseable but we
// want to skip the same number of frames regardless. I.e. we can't do it
// in the caller.
for (let i = skipFrames; i < frames.length; i++) {
const parsed = firefoxFrameRegExp.exec(frames[i]);
if (!parsed) {
continue;
}
const name = parsed[1] || '';
const filename = parsed[2] || '';
const line = +parsed[3];
const col = +parsed[4];
parsedFrames.push([name, filename, line, col, 0, 0, false]);
}
return parsedFrames;
}
const CHROME_STACK_REGEXP = /^\s*at /m;
export function parseStackTraceFromString(
stack: string,
skipFrames: number,
): ReactStackTrace {
if (stack.match(CHROME_STACK_REGEXP)) {
return parseStackTraceFromChromeStack(stack, skipFrames);
}
return parseStackTraceFromFirefoxStack(stack, skipFrames);
}
let framesToSkip: number = 0;
let collectedStackTrace: null | ReactStackTrace = null;
const identifierRegExp = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;
function getMethodCallName(callSite: CallSite): string {
const typeName = callSite.getTypeName();
const methodName = callSite.getMethodName();
const functionName = callSite.getFunctionName();
let result = '';
if (functionName) {
if (
typeName &&
identifierRegExp.test(functionName) &&
functionName !== typeName
) {
result += typeName + '.';
}
result += functionName;
if (
methodName &&
functionName !== methodName &&
!functionName.endsWith('.' + methodName) &&
!functionName.endsWith(' ' + methodName)
) {
result += ' [as ' + methodName + ']';
}
} else {
if (typeName) {
result += typeName + '.';
}
if (methodName) {
result += methodName;
} else {
result += '<anonymous>';
}
}
return result;
}
function collectStackTrace(
error: Error,
structuredStackTrace: CallSite[],
): string {
const result: ReactStackTrace = [];
// Collect structured stack traces from the callsites.
// We mirror how V8 serializes stack frames and how we later parse them.
for (let i = framesToSkip; i < structuredStackTrace.length; i++) {
const callSite = structuredStackTrace[i];
let name =
// $FlowFixMe[method-unbinding]
typeof callSite.getFunctionName === 'function'
? callSite.getFunctionName() || '<anonymous>'
: '';
if (
name.includes('react_stack_bottom_frame') ||
name.includes('react-stack-bottom-frame')
) {
// Skip everything after the bottom frame since it'll be internals.
break;
// $FlowFixMe[method-unbinding]
} else if (typeof callSite.isNative === 'function' && callSite.isNative()) {
const isAsync =
// $FlowFixMe[prop-missing]
// $FlowFixMe[incompatible-use]
typeof callSite.isAsync === 'function' && callSite.isAsync();
result.push([name, '', 0, 0, 0, 0, isAsync]);
} else {
// We encode complex function calls as if they're part of the function
// name since we cannot simulate the complex ones and they look the same
// as function names in UIs on the client as well as stacks.
if (
// $FlowFixMe[method-unbinding]
typeof callSite.isConstructor === 'function' &&
callSite.isConstructor()
) {
name = 'new ' + name;
} else if (
// $FlowFixMe[method-unbinding]
typeof callSite.isToplevel === 'function' &&
!callSite.isToplevel()
) {
name = getMethodCallName(callSite);
}
if (name === '<anonymous>') {
name = '';
}
let filename =
// $FlowFixMe[method-unbinding]
typeof callSite.getScriptNameOrSourceURL === 'function'
? callSite.getScriptNameOrSourceURL() || '<anonymous>'
: '';
if (filename === '<anonymous>') {
filename = '';
// $FlowFixMe[method-unbinding]
if (typeof callSite.isEval === 'function' && callSite.isEval()) {
const origin =
// $FlowFixMe[method-unbinding]
typeof callSite.getEvalOrigin === 'function'
? callSite.getEvalOrigin()
: null;
if (origin) {
filename = origin.toString() + ', <anonymous>';
}
}
}
const line =
// $FlowFixMe[method-unbinding]
(typeof callSite.getLineNumber === 'function' &&
callSite.getLineNumber()) ||
0;
const col =
// $FlowFixMe[method-unbinding]
(typeof callSite.getColumnNumber === 'function' &&
callSite.getColumnNumber()) ||
0;
const enclosingLine: number =
// $FlowFixMe[prop-missing]
typeof callSite.getEnclosingLineNumber === 'function'
? (callSite: any).getEnclosingLineNumber() || 0
: 0;
const enclosingCol: number =
// $FlowFixMe[prop-missing]
typeof callSite.getEnclosingColumnNumber === 'function'
? (callSite: any).getEnclosingColumnNumber() || 0
: 0;
const isAsync =
// $FlowFixMe[prop-missing]
// $FlowFixMe[incompatible-use]
typeof callSite.isAsync === 'function' && callSite.isAsync();
result.push([
name,
filename,
line,
col,
enclosingLine,
enclosingCol,
isAsync,
]);
}
}
collectedStackTrace = result;
// At the same time we generate a string stack trace just in case someone
// else reads it. Ideally, we'd call the previous prepareStackTrace to
// ensure it's in the expected format but it's common for that to be
// source mapped and since we do a lot of eager parsing of errors, it
// would be slow in those environments. We could maybe just rely on those
// environments having to disable source mapping globally to speed things up.
// For now, we just generate a default V8 formatted stack trace without
// source mapping as a fallback.
const name = error.name || 'Error';
const message = error.message || '';
let stack = name + ': ' + message;
for (let i = 0; i < structuredStackTrace.length; i++) {
stack += '\n at ' + structuredStackTrace[i].toString();
}
return stack;
}
// This matches either of these V8 formats.
// at name (filename:0:0)
// at filename:0:0
// at async filename:0:0
// at Array.map (<anonymous>)
const chromeFrameRegExp =
/^ *at (?:(.+) \((?:(.+):(\d+):(\d+)|\<anonymous\>)\)|(?:async )?(.+):(\d+):(\d+)|\<anonymous\>)$/;
const stackTraceCache: WeakMap<Error, ReactStackTrace> = new WeakMap();
export function parseStackTrace(
error: Error,
skipFrames: number,
): ReactStackTrace {
// We can only get structured data out of error objects once. So we cache the information
// so we can get it again each time. It also helps performance when the same error is
// referenced more than once.
const existing = stackTraceCache.get(error);
if (existing !== undefined) {
return existing;
}
// We override Error.prepareStackTrace with our own version that collects
// the structured data. We need more information than the raw stack gives us
// and we need to ensure that we don't get the source mapped version.
collectedStackTrace = null;
framesToSkip = skipFrames;
const previousPrepare = Error.prepareStackTrace;
Error.prepareStackTrace = collectStackTrace;
let stack;
try {
stack = String(error.stack);
} finally {
Error.prepareStackTrace = previousPrepare;
}
if (collectedStackTrace !== null) {
const result = collectedStackTrace;
collectedStackTrace = null;
stackTraceCache.set(error, result);
return result;
}
// If the stack has already been read, or this is not actually a V8 compatible
// engine then we might not get a normalized stack and it might still have been
// source mapped. Regardless we try our best to parse it.
const parsedFrames = parseStackTraceFromString(stack, skipFrames);
stackTraceCache.set(error, parsedFrames);
return parsedFrames;
}
export function extractLocationFromOwnerStack(
error: Error,
): ReactFunctionLocation | null {
const stackTrace = parseStackTrace(error, 1);
const stack = error.stack;
if (
!stack.includes('react_stack_bottom_frame') &&
!stack.includes('react-stack-bottom-frame')
) {
// This didn't have a bottom to it, we can't trust it.
return null;
}
// We start from the bottom since that will have the best location for the owner itself.
for (let i = stackTrace.length - 1; i >= 0; i--) {
const [functionName, fileName, line, col, encLine, encCol] = stackTrace[i];
// Take the first match with a colon in the file name.
if (fileName.indexOf(':') !== -1) {
return [
functionName,
fileName,
// Use enclosing line if available, since that points to the start of the function.
encLine || line,
encCol || col,
];
}
}
return null;
}
export function extractLocationFromComponentStack(
stack: string,
): ReactFunctionLocation | null {
const stackTrace = parseStackTraceFromString(stack, 0);
for (let i = 0; i < stackTrace.length; i++) {
const [functionName, fileName, line, col, encLine, encCol] = stackTrace[i];
// Take the first match with a colon in the file name.
if (fileName.indexOf(':') !== -1) {
return [
functionName,
fileName,
// Use enclosing line if available. (Never the case here because we parse from string.)
encLine || line,
encCol || col,
];
}
}
return null;
}