Skip to content

Commit

Permalink
Removes the digest property from errorInfo passed to onRecoverableErr…
Browse files Browse the repository at this point in the history
…or when handling an error propagated from the server. Previously we warned in Dev but still provided the digest on the errorInfo object. This change removes digest from error info but continues to warn if it is accessed. The reason for retaining the warning is the version with the warning was not released as stable but we will include this deprecated removal in our next major so we should communicate this change at runtime.
  • Loading branch information
gnoff committed Feb 2, 2024
1 parent 2efa383 commit 6000fee
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
8 changes: 4 additions & 4 deletions packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3621,11 +3621,11 @@ describe('ReactDOMFizzServer', () => {
onRecoverableError(error, errorInfo) {
expect(() => {
expect(error.digest).toBe('a digest');
expect(errorInfo.digest).toBe('a digest');
expect(errorInfo.digest).toBe(undefined);
}).toErrorDev(
'Warning: 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.',
'You are accessing "digest" from the errorInfo object passed to onRecoverableError.' +
' This property is no longer provided as part of errorInfo but can be accessed as a property' +
' of the Error instance itself.',
{withoutStack: true},
);
},
Expand Down
40 changes: 22 additions & 18 deletions packages/react-reconciler/src/ReactFiberWorkLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -3001,10 +3001,7 @@ function commitRootImpl(
const onRecoverableError = root.onRecoverableError;
for (let i = 0; i < recoverableErrors.length; i++) {
const recoverableError = recoverableErrors[i];
const errorInfo = makeErrorInfo(
recoverableError.digest,
recoverableError.stack,
);
const errorInfo = makeErrorInfo(recoverableError.stack);
onRecoverableError(recoverableError.value, errorInfo);
}
}
Expand Down Expand Up @@ -3104,28 +3101,35 @@ function commitRootImpl(
return null;
}

function makeErrorInfo(digest: ?string, componentStack: ?string) {
function makeErrorInfo(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 new Proxy(errorInfo, {
get(target, prop, receiver) {
if (prop === 'digest') {
console.error(
'You are accessing "digest" from the errorInfo object passed to onRecoverableError.' +
' This property is no longer provided as part of errorInfo but can be accessed as a property' +
' of the Error instance itself.',
);
}
return Reflect.get(target, prop, receiver);
},
has(target, prop) {
if (prop === 'digest') {
console.error(
'You are accessing "digest" from the errorInfo object passed to onRecoverableError.' +
' This property is no longer provided as part of errorInfo but can be accessed as a property' +
' of the Error instance itself.',
);
}
return Reflect.has(target, prop);
},
});
return errorInfo;
} else {
return {
digest,
componentStack,
};
}
Expand Down

0 comments on commit 6000fee

Please sign in to comment.