| 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 | // This is a DevTools fork of shared/ReactOwnerStackFrames. |
| 11 | |
| 12 | export function formatOwnerStack(error: Error): string { |
| 13 | const prevPrepareStackTrace = Error.prepareStackTrace; |
| 14 | // $FlowFixMe[incompatible-type] It does accept undefined. |
| 15 | Error.prepareStackTrace = undefined; |
| 16 | let stack = error.stack; |
| 17 | Error.prepareStackTrace = prevPrepareStackTrace; |
| 18 | |
| 19 | if (stack.startsWith('Error: react-stack-top-frame\n')) { |
| 20 | // V8's default formatting prefixes with the error message which we |
| 21 | // don't want/need. |
| 22 | stack = stack.slice(29); |
| 23 | } |
| 24 | let idx = stack.indexOf('\n'); |
| 25 | if (idx !== -1) { |
| 26 | // Pop the JSX frame. |
| 27 | stack = stack.slice(idx + 1); |
| 28 | } |
| 29 | idx = stack.indexOf('react_stack_bottom_frame'); |
| 30 | if (idx === -1) { |
| 31 | idx = stack.indexOf('react-stack-bottom-frame'); |
| 32 | } |
| 33 | if (idx !== -1) { |
| 34 | idx = stack.lastIndexOf('\n', idx); |
| 35 | } |
| 36 | if (idx !== -1) { |
| 37 | // Cut off everything after the bottom frame since it'll be internals. |
| 38 | stack = stack.slice(0, idx); |
| 39 | } else { |
| 40 | // We didn't find any internal callsite out to user space. |
| 41 | // This means that this was called outside an owner or the owner is fully internal. |
| 42 | // To keep things light we exclude the entire trace in this case. |
| 43 | return ''; |
| 44 | } |
| 45 | return stack; |
| 46 | } |