main
js 41 lines 1.31 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 import DefaultPrepareStackTrace from 'shared/DefaultPrepareStackTrace';
11
12 export function formatOwnerStack(error: Error): string {
13 const prevPrepareStackTrace = Error.prepareStackTrace;
14 Error.prepareStackTrace = DefaultPrepareStackTrace;
15 let stack = error.stack;
16 Error.prepareStackTrace = prevPrepareStackTrace;
17 if (stack.startsWith('Error: react-stack-top-frame\n')) {
18 // V8's default formatting prefixes with the error message which we
19 // don't want/need.
20 stack = stack.slice(29);
21 }
22 let idx = stack.indexOf('\n');
23 if (idx !== -1) {
24 // Pop the JSX frame.
25 stack = stack.slice(idx + 1);
26 }
27 idx = stack.indexOf('react_stack_bottom_frame');
28 if (idx !== -1) {
29 idx = stack.lastIndexOf('\n', idx);
30 }
31 if (idx !== -1) {
32 // Cut off everything after the bottom frame since it'll be internals.
33 stack = stack.slice(0, idx);
34 } else {
35 // We didn't find any internal callsite out to user space.
36 // This means that this was called outside an owner or the owner is fully internal.
37 // To keep things light we exclude the entire trace in this case.
38 return '';
39 }
40 return stack;
41 }