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

[Flight] Error names are not preserved #31335

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
51 changes: 51 additions & 0 deletions packages/react-client/src/__tests__/ReactFlight-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,57 @@ describe('ReactFlight', () => {
}
});

it('preserves Error names', async () => {
// This doesn't cover console replays which display something different.
// E.g. `console.log(new NamedError())` would display `NamedError [MyError]: ...`
// `console.log(new UnnamedError())` would display `UnnamedError: ...`
class UnnamedError extends Error {}
class NamedError extends Error {
name: 'MyError';
}

function ComponentClient({typeError, namedError, unnamedError}) {
return `
type: ${typeError.stack.split('\n')[0]}
named: ${namedError.stack.split('\n')[0]}
unnamed: ${unnamedError.stack.split('\n')[0]}
`;
}
const Component = clientReference(ComponentClient);

function ServerComponent() {
return (
<Component
typeError={new TypeError('Foo')}
namedError={new NamedError('Bar')}
unnamedError={new UnnamedError('Baz')}
/>
);
}

const transport = ReactNoopFlightServer.render(<ServerComponent />);

await act(async () => {
ReactNoop.render(await ReactNoopFlightClient.read(transport));
});

if (__DEV__) {
// `error.stack` ignores constructor names and `.name`.
// only built-in errors have their constructor name used.
expect(ReactNoop).toMatchRenderedOutput(`
type: TypeError: Foo
named: Error: Bar
unnamed: Error: Baz
`);
} else {
expect(ReactNoop).toMatchRenderedOutput(`
type: Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In prod, we should probably not preserve the original constructor to not leak implementation details.

named: Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.
unnamed: Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.
`);
}
});

it('can transport cyclic objects', async () => {
function ComponentClient({prop}) {
expect(prop.obj.obj.obj).toBe(prop.obj.obj);
Expand Down
Loading