mirror of
https://github.com/zebrajr/react.git
synced 2025-12-06 12:20:20 +01:00
We don't actually want the source mapped version of `.stack` from errors because that would cause us to not be able to associate it with a source map in the UIs that need it. The strategy in browsers is more correct where the display is responsible for source maps. That's why we disable any custom `prepareStackTrace` like the ones added by `source-map`. We reset it to `undefined`. However, when running node with `--enable-source-maps` the default for `prepareStackTrace` which is a V8 feature (but may exist elsewhere too like Bun) is a source mapped version of the stack. In those environments we need to reset it to a default implementation that doesn't apply source maps. We already did this in Flight using the `ReactFlightStackConfigV8.js` config. However, we need this more generally in the `shared/ReactComponentStackFrame` implementation. We could always set it to the default implementation instead of `undefined` but that's unnecessary code in browser builds and it might lead to slightly different results. For safety and code size, this PR does it with a fork instead. All builds specific to `node` or `edge` (or `markup` which is a server feature) gets the default implementation where as everything else (e.g. browsers) get `undefined` since it's expected that this is not source mapped. We don't have to do anything about the equivalent in React DevTools since React DevTools doesn't run on the server.
42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
/**
|
|
* 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 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
|
|
// don't want/need.
|
|
stack = stack.slice(29);
|
|
}
|
|
let idx = stack.indexOf('\n');
|
|
if (idx !== -1) {
|
|
// Pop the JSX frame.
|
|
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;
|
|
}
|