diff --git a/packages/react-reconciler/src/ReactFiberErrorLogger.js b/packages/react-reconciler/src/ReactFiberErrorLogger.js index addb0aea43d..59af033b568 100644 --- a/packages/react-reconciler/src/ReactFiberErrorLogger.js +++ b/packages/react-reconciler/src/ReactFiberErrorLogger.js @@ -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 && @@ -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.