Skip to content

Commit

Permalink
[Fiber] Override the getCurrentStack temporarily while printing errors (
Browse files Browse the repository at this point in the history
#30300)

Only for parent stacks. This ensures that we can use the regular
mechanism for appending stack traces. E.g. you can polyfill it.

This is mainly so that I can later remove the automatic appending.
  • Loading branch information
sebmarkbage authored Jul 10, 2024
1 parent 39e69dc commit 3b2e5f2
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions packages/react-reconciler/src/ReactFiberErrorLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,19 @@ export function defaultOnCaughtError(
errorBoundaryName || 'Anonymous'
}.`;

if (enableOwnerStacks) {
const prevGetCurrentStack = ReactSharedInternals.getCurrentStack;
if (!enableOwnerStacks) {
// The current Fiber is disconnected at this point which means that console printing
// cannot add a component stack since it terminates at the deletion node. This is not
// a problem for owner stacks which are not disconnected but for the parent component
// stacks we need to use the snapshot we've previously extracted.
const componentStack =
errorInfo.componentStack != null ? errorInfo.componentStack : '';
ReactSharedInternals.getCurrentStack = function () {
return componentStack;
};
}
try {
if (
typeof error === 'object' &&
error !== null &&
Expand Down Expand Up @@ -123,21 +135,10 @@ export function defaultOnCaughtError(
// We let our consoleWithStackDev wrapper add the component stack to the end.
);
}
} else {
// The current Fiber is disconnected at this point which means that console printing
// cannot add a component stack since it terminates at the deletion node. This is not
// a problem for owner stacks which are not disconnected but for the parent component
// stacks we need to use the snapshot we've previously extracted.
const componentStack =
errorInfo.componentStack != null ? errorInfo.componentStack : '';
// Don't transform to our wrapper
console['error'](
'%o\n\n%s\n\n%s\n%s',
error,
componentNameMessage,
recreateMessage,
componentStack,
);
} finally {
if (!enableOwnerStacks) {
ReactSharedInternals.getCurrentStack = prevGetCurrentStack;
}
}
} else {
// In production, we print the error directly.
Expand Down

0 comments on commit 3b2e5f2

Please sign in to comment.