diff --git a/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js b/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js index 0726209a2a3b1..225d3657e0469 100644 --- a/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js @@ -3257,6 +3257,7 @@ describe('ReactDOMFizzServer', () => { { onRecoverableError(error, errorInfo) { expect(() => { + expect(error.digest).toBe('a digest'); expect(errorInfo.digest).toBe('a digest'); }).toErrorDev( 'Warning: You are accessing "digest" from the errorInfo object passed to onRecoverableError.' + diff --git a/packages/react-reconciler/src/ReactFiberWorkLoop.new.js b/packages/react-reconciler/src/ReactFiberWorkLoop.new.js index 74a4e8288002e..6d244630867b2 100644 --- a/packages/react-reconciler/src/ReactFiberWorkLoop.new.js +++ b/packages/react-reconciler/src/ReactFiberWorkLoop.new.js @@ -2596,24 +2596,10 @@ function commitRootImpl( const onRecoverableError = root.onRecoverableError; for (let i = 0; i < recoverableErrors.length; i++) { const recoverableError = recoverableErrors[i]; - const componentStack = recoverableError.stack; - const digest = recoverableError.digest; - let errorInfo; - if (__DEV__) { - errorInfo = { - componentStack, - get digest() { - console.error( - 'You are accessing "digest" from the errorInfo object passed to onRecoverableError.' + - ' This property is deprecated and will be removed in a future version of React.' + - ' To access the digest of an Error look for this property on the Error instance itself.', - ); - return digest; - }, - }; - } else { - errorInfo = {componentStack, digest}; - } + const errorInfo = makeErrorInfo( + recoverableError.digest, + recoverableError.stack, + ); onRecoverableError(recoverableError.value, errorInfo); } } @@ -2705,6 +2691,33 @@ function commitRootImpl( return null; } +function makeErrorInfo(digest: ?string, componentStack: ?string) { + if (__DEV__) { + const errorInfo = { + componentStack, + digest, + }; + Object.defineProperty(errorInfo, 'digest', { + configurable: false, + enumerable: true, + get() { + console.error( + 'You are accessing "digest" from the errorInfo object passed to onRecoverableError.' + + ' This property is deprecated and will be removed in a future version of React.' + + ' To access the digest of an Error look for this property on the Error instance itself.', + ); + return digest; + }, + }); + return errorInfo; + } else { + return { + digest, + componentStack, + }; + } +} + function releaseRootPooledCache(root: FiberRoot, remainingLanes: Lanes) { if (enableCache) { const pooledCacheLanes = (root.pooledCacheLanes &= remainingLanes); diff --git a/packages/react-reconciler/src/ReactFiberWorkLoop.old.js b/packages/react-reconciler/src/ReactFiberWorkLoop.old.js index 76661bd0c69d6..36799208dc69b 100644 --- a/packages/react-reconciler/src/ReactFiberWorkLoop.old.js +++ b/packages/react-reconciler/src/ReactFiberWorkLoop.old.js @@ -2596,24 +2596,10 @@ function commitRootImpl( const onRecoverableError = root.onRecoverableError; for (let i = 0; i < recoverableErrors.length; i++) { const recoverableError = recoverableErrors[i]; - const componentStack = recoverableError.stack; - const digest = recoverableError.digest; - let errorInfo; - if (__DEV__) { - errorInfo = { - componentStack, - get digest() { - console.error( - 'You are accessing "digest" from the errorInfo object passed to onRecoverableError.' + - ' This property is deprecated and will be removed in a future version of React.' + - ' To access the digest of an Error look for this property on the Error instance itself.', - ); - return digest; - }, - }; - } else { - errorInfo = {componentStack, digest}; - } + const errorInfo = makeErrorInfo( + recoverableError.digest, + recoverableError.stack, + ); onRecoverableError(recoverableError.value, errorInfo); } } @@ -2705,6 +2691,33 @@ function commitRootImpl( return null; } +function makeErrorInfo(digest: ?string, componentStack: ?string) { + if (__DEV__) { + const errorInfo = { + componentStack, + digest, + }; + Object.defineProperty(errorInfo, 'digest', { + configurable: false, + enumerable: true, + get() { + console.error( + 'You are accessing "digest" from the errorInfo object passed to onRecoverableError.' + + ' This property is deprecated and will be removed in a future version of React.' + + ' To access the digest of an Error look for this property on the Error instance itself.', + ); + return digest; + }, + }); + return errorInfo; + } else { + return { + digest, + componentStack, + }; + } +} + function releaseRootPooledCache(root: FiberRoot, remainingLanes: Lanes) { if (enableCache) { const pooledCacheLanes = (root.pooledCacheLanes &= remainingLanes);