Skip to content

Commit a2f917d

Browse files
committed
fix closure escape
1 parent 2caef37 commit a2f917d

File tree

3 files changed

+63
-36
lines changed

3 files changed

+63
-36
lines changed

packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3257,6 +3257,7 @@ describe('ReactDOMFizzServer', () => {
32573257
{
32583258
onRecoverableError(error, errorInfo) {
32593259
expect(() => {
3260+
expect(error.digest).toBe('a digest');
32603261
expect(errorInfo.digest).toBe('a digest');
32613262
}).toErrorDev(
32623263
'Warning: You are accessing "digest" from the errorInfo object passed to onRecoverableError.' +

packages/react-reconciler/src/ReactFiberWorkLoop.new.js

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,24 +2596,10 @@ function commitRootImpl(
25962596
const onRecoverableError = root.onRecoverableError;
25972597
for (let i = 0; i < recoverableErrors.length; i++) {
25982598
const recoverableError = recoverableErrors[i];
2599-
const componentStack = recoverableError.stack;
2600-
const digest = recoverableError.digest;
2601-
let errorInfo;
2602-
if (__DEV__) {
2603-
errorInfo = {
2604-
componentStack,
2605-
get digest() {
2606-
console.error(
2607-
'You are accessing "digest" from the errorInfo object passed to onRecoverableError.' +
2608-
' This property is deprecated and will be removed in a future version of React.' +
2609-
' To access the digest of an Error look for this property on the Error instance itself.',
2610-
);
2611-
return digest;
2612-
},
2613-
};
2614-
} else {
2615-
errorInfo = {componentStack, digest};
2616-
}
2599+
const errorInfo = makeErrorInfo(
2600+
recoverableError.digest,
2601+
recoverableError.stack,
2602+
);
26172603
onRecoverableError(recoverableError.value, errorInfo);
26182604
}
26192605
}
@@ -2705,6 +2691,33 @@ function commitRootImpl(
27052691
return null;
27062692
}
27072693

2694+
function makeErrorInfo(digest: ?string, componentStack: ?string) {
2695+
if (__DEV__) {
2696+
const errorInfo = {
2697+
componentStack,
2698+
digest,
2699+
};
2700+
Object.defineProperty(errorInfo, 'digest', {
2701+
configurable: false,
2702+
enumerable: true,
2703+
get() {
2704+
console.error(
2705+
'You are accessing "digest" from the errorInfo object passed to onRecoverableError.' +
2706+
' This property is deprecated and will be removed in a future version of React.' +
2707+
' To access the digest of an Error look for this property on the Error instance itself.',
2708+
);
2709+
return digest;
2710+
},
2711+
});
2712+
return errorInfo;
2713+
} else {
2714+
return {
2715+
digest,
2716+
componentStack,
2717+
};
2718+
}
2719+
}
2720+
27082721
function releaseRootPooledCache(root: FiberRoot, remainingLanes: Lanes) {
27092722
if (enableCache) {
27102723
const pooledCacheLanes = (root.pooledCacheLanes &= remainingLanes);

packages/react-reconciler/src/ReactFiberWorkLoop.old.js

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,24 +2596,10 @@ function commitRootImpl(
25962596
const onRecoverableError = root.onRecoverableError;
25972597
for (let i = 0; i < recoverableErrors.length; i++) {
25982598
const recoverableError = recoverableErrors[i];
2599-
const componentStack = recoverableError.stack;
2600-
const digest = recoverableError.digest;
2601-
let errorInfo;
2602-
if (__DEV__) {
2603-
errorInfo = {
2604-
componentStack,
2605-
get digest() {
2606-
console.error(
2607-
'You are accessing "digest" from the errorInfo object passed to onRecoverableError.' +
2608-
' This property is deprecated and will be removed in a future version of React.' +
2609-
' To access the digest of an Error look for this property on the Error instance itself.',
2610-
);
2611-
return digest;
2612-
},
2613-
};
2614-
} else {
2615-
errorInfo = {componentStack, digest};
2616-
}
2599+
const errorInfo = makeErrorInfo(
2600+
recoverableError.digest,
2601+
recoverableError.stack,
2602+
);
26172603
onRecoverableError(recoverableError.value, errorInfo);
26182604
}
26192605
}
@@ -2705,6 +2691,33 @@ function commitRootImpl(
27052691
return null;
27062692
}
27072693

2694+
function makeErrorInfo(digest: ?string, componentStack: ?string) {
2695+
if (__DEV__) {
2696+
const errorInfo = {
2697+
componentStack,
2698+
digest,
2699+
};
2700+
Object.defineProperty(errorInfo, 'digest', {
2701+
configurable: false,
2702+
enumerable: true,
2703+
get() {
2704+
console.error(
2705+
'You are accessing "digest" from the errorInfo object passed to onRecoverableError.' +
2706+
' This property is deprecated and will be removed in a future version of React.' +
2707+
' To access the digest of an Error look for this property on the Error instance itself.',
2708+
);
2709+
return digest;
2710+
},
2711+
});
2712+
return errorInfo;
2713+
} else {
2714+
return {
2715+
digest,
2716+
componentStack,
2717+
};
2718+
}
2719+
}
2720+
27082721
function releaseRootPooledCache(root: FiberRoot, remainingLanes: Lanes) {
27092722
if (enableCache) {
27102723
const pooledCacheLanes = (root.pooledCacheLanes &= remainingLanes);

0 commit comments

Comments
 (0)