Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fizz] Move digest from errorInfo to Error instance #25313

Merged
merged 2 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('ReactDOMFizzServer', () => {
function expectErrors(errorsArr, toBeDevArr, toBeProdArr) {
const mappedErrows = errorsArr.map(({error, errorInfo}) => {
const stack = errorInfo && errorInfo.componentStack;
const digest = errorInfo && errorInfo.digest;
const digest = error.digest;
if (stack) {
return [error.message, digest, normalizeCodeLocInfo(stack)];
} else if (digest) {
Expand Down Expand Up @@ -3230,6 +3230,46 @@ describe('ReactDOMFizzServer', () => {
);
});

it('warns in dev if you access digest from errorInfo in onRecoverableError', async () => {
await act(async () => {
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(
<div>
<Suspense fallback={'loading...'}>
<AsyncText text={'hello'} />
</Suspense>
</div>,
{
onError(error) {
return 'a digest';
},
},
);
rejectText('hello');
pipe(writable);
});
expect(getVisibleChildren(container)).toEqual(<div>loading...</div>);

ReactDOMClient.hydrateRoot(
container,
<div>
<Suspense fallback={'loading...'}>hello</Suspense>
</div>,
{
onRecoverableError(error, errorInfo) {
expect(() => {
expect(errorInfo.digest).toBe('a digest');
}).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.',
{withoutStack: true},
);
},
},
);
expect(Scheduler).toFlushWithoutYielding();
});

describe('error escaping', () => {
it('escapes error hash, message, and component stack values in directly flushed errors (html escaping)', async () => {
window.__outlet = {};
Expand Down
1 change: 1 addition & 0 deletions packages/react-reconciler/src/ReactFiberBeginWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2746,6 +2746,7 @@ function updateDehydratedSuspenseComponent(
'client rendering.',
);
}
(error: any).digest = digest;
const capturedValue = createCapturedValue(error, digest, stack);
return retrySuspenseComponentWithoutHydrating(
current,
Expand Down
1 change: 1 addition & 0 deletions packages/react-reconciler/src/ReactFiberBeginWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -2746,6 +2746,7 @@ function updateDehydratedSuspenseComponent(
'client rendering.',
);
}
(error: any).digest = digest;
const capturedValue = createCapturedValue(error, digest, stack);
return retrySuspenseComponentWithoutHydrating(
current,
Expand Down
18 changes: 17 additions & 1 deletion packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2598,7 +2598,23 @@ function commitRootImpl(
const recoverableError = recoverableErrors[i];
const componentStack = recoverableError.stack;
const digest = recoverableError.digest;
onRecoverableError(recoverableError.value, {componentStack, 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};
}
onRecoverableError(recoverableError.value, errorInfo);
}
}

Expand Down
18 changes: 17 additions & 1 deletion packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -2598,7 +2598,23 @@ function commitRootImpl(
const recoverableError = recoverableErrors[i];
const componentStack = recoverableError.stack;
const digest = recoverableError.digest;
onRecoverableError(recoverableError.value, {componentStack, 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};
}
onRecoverableError(recoverableError.value, errorInfo);
}
}

Expand Down