ReactOwnerStackFrames React所有者堆栈帧
格式化所有者堆栈
import DefaultPrepareStackTrace from 'shared/DefaultPrepareStackTrace';
export function formatOwnerStack(error: Error): string {
const prevPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = DefaultPrepareStackTrace;
let stack = error.stack;
Error.prepareStackTrace = prevPrepareStackTrace;
if (stack.startsWith('Error: react-stack-top-frame\n')) {
// V8's default formatting prefixes with the error message which we
// V8 默认的格式化会在错误信息前加上前缀,而我们不想要/不需要这个前缀。
// don't want/need.
stack = stack.slice(29);
}
let idx = stack.indexOf('\n');
if (idx !== -1) {
// Pop the JSX frame.
// 弹出 JSX 框架。
stack = stack.slice(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);
} else {
// We didn't find any internal callsite out to user space.
// 我们没有发现任何调用到用户空间的内部调用点。
// This means that this was called outside an owner or the owner is fully internal.
// 这意味着这是在所有者之外调用的,或者所有者是完全内部的。
// To keep things light we exclude the entire trace in this case.
// 为了保持轻量,我们在这种情况下排除了整个调用跟踪。
return '';
}
return stack;
}