Skip to content

[Flight] Set Current Owner / Task When Calling console.error or invoking onError/onPostpone #30206

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

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
63 changes: 62 additions & 1 deletion packages/react-client/src/__tests__/ReactFlight-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2761,10 +2761,61 @@ describe('ReactFlight', () => {
);
});

// @gate __DEV__ && enableOwnerStacks
it('can get the component owner stacks for onError in dev', async () => {
const thrownError = new Error('hi');
let caughtError;
let ownerStack;

function Foo() {
return ReactServer.createElement(Bar, null);
}
function Bar() {
return ReactServer.createElement(
'div',
null,
ReactServer.createElement(Baz, null),
);
}
function Baz() {
throw thrownError;
}

ReactNoopFlightServer.render(
ReactServer.createElement(
'div',
null,
ReactServer.createElement(Foo, null),
),
{
onError(error, errorInfo) {
caughtError = error;
ownerStack = ReactServer.captureOwnerStack
? ReactServer.captureOwnerStack()
: null;
Comment on lines +2793 to +2795
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is for a more useful assertion failure when we run this test without the flag?

Suggested change
ownerStack = ReactServer.captureOwnerStack
? ReactServer.captureOwnerStack()
: null;
ownerStack = gate(flags => flags.enableOwnerStacks)
? ReactServer.captureOwnerStack()
: null;

so that we don't miss this when cleaning up the flag?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just copy paste from another case but this test is a bit weird in that it's gated on DEV and not just ignored in DEV.

},
},
);

expect(caughtError).toBe(thrownError);
expect(normalizeCodeLocInfo(ownerStack)).toBe(
'\n in Bar (at **)' + '\n in Foo (at **)',
);
});

// @gate (enableOwnerStacks && enableServerComponentLogs) || !__DEV__
it('should not include component stacks in replayed logs (unless DevTools add them)', () => {
class MyError extends Error {
toJSON() {
return 123;
}
}

function Foo() {
return 'hi';
return ReactServer.createElement('div', null, [
'Womp womp: ',
new MyError('spaghetti'),
]);
}

function Bar() {
Expand All @@ -2781,11 +2832,18 @@ describe('ReactFlight', () => {
const transport = ReactNoopFlightServer.render(
ReactServer.createElement(App),
);

assertConsoleErrorDev([
'Each child in a list should have a unique "key" prop.' +
' See https://react.dev/link/warning-keys for more information.\n' +
' in Bar (at **)\n' +
' in App (at **)',
'Error objects cannot be rendered as text children. Try formatting it using toString().\n' +
' <div>Womp womp: {Error}</div>\n' +
' ^^^^^^^\n' +
' in Foo (at **)\n' +
' in Bar (at **)\n' +
' in App (at **)',
]);

// Replay logs on the client
Expand All @@ -2794,6 +2852,9 @@ describe('ReactFlight', () => {
[
'Each child in a list should have a unique "key" prop.' +
' See https://react.dev/link/warning-keys for more information.',
'Error objects cannot be rendered as text children. Try formatting it using toString().\n' +
' <div>Womp womp: {Error}</div>\n' +
' ^^^^^^^',
],
// We should not have a stack in the replay because that should be added either by console.createTask
// or React DevTools on the client. Neither of which we do here.
Expand Down
Loading