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

[Fiber] Don't Rethrow Errors at the Root #28627

Merged
merged 24 commits into from
Mar 27, 2024

Conversation

sebmarkbage
Copy link
Collaborator

@sebmarkbage sebmarkbage commented Mar 25, 2024

Stacked on top of #28498 for test fixes.

Don't Rethrow

When we started React it was 1:1 setState calls a series of renders and if they error, it errors where the setState was called. Simple. However, then batching came and the error actually got thrown somewhere else. With concurrent mode, it's not even possible to get setState itself to throw anymore.

In fact, all APIs that can rethrow out of React are executed either at the root of the scheduler or inside a DOM event handler.
If you throw inside a React.startTransition callback that's sync, then that will bubble out of the startTransition but if you throw inside an async callback or a useTransition we now need to handle it at the hook site. So in 19 we need to make all React.startTransition swallow the error (and report them to reportError).

The only one remaining that can throw is flushSync but it doesn't really make sense for it to throw at the callsite neither because batching. Just because something rendered in this flush doesn't mean it was rendered due to what was just scheduled and doesn't mean that it should abort any of the remaining code afterwards. setState is fire and forget. It's send an instruction elsewhere, it's not part of the current imperative code.

Error boundaries never rethrow. Since you should really always have error boundaries, most of the time, it wouldn't rethrow anyway.

Rethrowing also actually currently drops errors on the floor since we can only rethrow the first error, so to avoid that we'd need to call reportError anyway. This happens in RN events.

The other issue with rethrowing is that it logs an extra console.error. Since we're not sure that user code will actually log it anywhere we still log it too just like we do with errors inside error boundaries which leads all of these to log twice.
The goal of this PR is to never rethrow out of React instead, errors outside of error boundaries get logged to reportError. Event system errors too.

Breaking Changes

The main thing this affects is testing where you want to inspect the errors thrown. To make it easier to port, if you're inside act we track the error into act in an aggregate error and then rethrow it at the root of act. Unlike before though, if you flush synchronously inside of act it'll still continue until the end of act before rethrowing.

I expect most user code breakages would be to migrate from flushSync to act if you assert on throwing.

However, in the React repo we also have internalAct and the waitForThrow helpers. Since these have to use public production implementations we track these using the global onerror or process uncaughtException. Unlike regular act, includes both event handler errors and onRecoverableError by default too. Not just render/commit errors. So I had to account for that in our tests.

We restore logging an extra log for uncaught errors after the main log with the component stack in it. We use console.warn. This is not yet ignorable if you preventDefault to the main error event. To avoid confusion if you don't end up logging the error to console I just added An error occurred.

Polyfill

All browsers we support really supports reportError but not all test and server environments do, so I implemented a polyfill for browser and node in shared/reportGlobalError. I don't love that this is included in all builds and gets duplicated into isomorphic even though it's not actually needed in production. Maybe in the future we can require a polyfill for this.

Follow Ups

In a follow up, I'll make caught vs uncaught error handling be configurable too.

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Mar 25, 2024
logCapturedError(
root.current,
createCapturedValueAtFiber(error, root.current),
);
Copy link
Collaborator Author

@sebmarkbage sebmarkbage Mar 25, 2024

Choose a reason for hiding this comment

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

Instead of stashing this and then logging, I just log it directly. I think that's fine since nothing else is really supposed to be happening in between but not sure. We can do that now since it's not rethrowing so we still will finish the commit after the log.

@react-sizebot
Copy link

react-sizebot commented Mar 25, 2024

Comparing: 5910eb3...0928d91

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js +0.12% 175.85 kB 176.06 kB = 54.68 kB 54.58 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js +0.12% 172.33 kB 172.54 kB = 53.81 kB 53.76 kB
facebook-www/ReactDOM-prod.classic.js = 589.44 kB 588.24 kB = 103.72 kB 103.41 kB
facebook-www/ReactDOM-prod.modern.js = 572.97 kB 571.76 kB = 100.75 kB 100.45 kB
oss-stable-semver/react/cjs/react.react-server.production.min.js +4.95% 7.63 kB 8.01 kB +4.01% 3.15 kB 3.27 kB
oss-stable/react/cjs/react.react-server.production.min.js +4.94% 7.66 kB 8.04 kB +4.00% 3.17 kB 3.30 kB
facebook-www/ReactServer-prod.modern.js +4.83% 16.84 kB 17.65 kB +4.35% 4.41 kB 4.61 kB
oss-stable-semver/react/cjs/react.production.min.js +4.42% 8.66 kB 9.05 kB +3.97% 3.25 kB 3.38 kB
oss-stable/react/cjs/react.production.min.js +4.41% 8.69 kB 9.07 kB +4.00% 3.27 kB 3.40 kB
oss-experimental/react/cjs/react.production.min.js +3.97% 9.64 kB 10.02 kB +3.72% 3.57 kB 3.71 kB
oss-experimental/react/cjs/react.react-server.production.min.js +3.95% 9.70 kB 10.08 kB +3.29% 3.86 kB 3.99 kB
facebook-react-native/react/cjs/React-prod.js +3.70% 20.53 kB 21.29 kB +3.87% 5.15 kB 5.35 kB
facebook-www/React-prod.modern.js +3.69% 20.60 kB 21.36 kB +3.73% 5.15 kB 5.34 kB
facebook-react-native/react/cjs/React-profiling.js +3.67% 20.67 kB 21.43 kB +3.82% 5.16 kB 5.36 kB
facebook-www/React-prod.classic.js +3.63% 20.88 kB 21.64 kB +3.68% 5.22 kB 5.41 kB
facebook-www/React-profiling.modern.js +3.61% 21.03 kB 21.79 kB +3.69% 5.23 kB 5.43 kB
facebook-www/React-profiling.classic.js +3.56% 21.32 kB 22.08 kB +3.62% 5.30 kB 5.49 kB
oss-stable-semver/react/umd/react.profiling.min.js +3.12% 12.27 kB 12.65 kB +2.60% 4.73 kB 4.86 kB
oss-stable-semver/react/umd/react.production.min.js +3.12% 12.27 kB 12.65 kB +2.60% 4.73 kB 4.86 kB
oss-stable/react/umd/react.profiling.min.js +3.12% 12.29 kB 12.68 kB +2.65% 4.76 kB 4.88 kB
oss-stable/react/umd/react.production.min.js +3.12% 12.30 kB 12.68 kB +2.63% 4.76 kB 4.88 kB
oss-experimental/react/umd/react.profiling.min.js +2.90% 13.19 kB 13.57 kB +2.44% 5.04 kB 5.16 kB
oss-experimental/react/umd/react.production.min.js +2.90% 13.19 kB 13.57 kB +2.44% 5.04 kB 5.16 kB
facebook-react-native/react/cjs/React-dev.js +2.31% 124.24 kB 127.11 kB +1.28% 29.63 kB 30.01 kB
facebook-www/React-dev.modern.js +2.30% 124.59 kB 127.46 kB +1.29% 29.63 kB 30.01 kB
oss-stable-semver/react/cjs/react.development.js +2.30% 98.80 kB 101.07 kB +1.38% 26.56 kB 26.93 kB
oss-stable/react/cjs/react.development.js +2.30% 98.83 kB 101.10 kB +1.37% 26.60 kB 26.96 kB
facebook-www/React-dev.classic.js +2.27% 126.17 kB 129.04 kB +1.32% 30.01 kB 30.40 kB
oss-experimental/react/cjs/react.development.js +2.27% 100.14 kB 102.41 kB +1.34% 27.24 kB 27.60 kB
test_utils/ReactAllWarnings.js Deleted 65.29 kB 0.00 kB Deleted 16.34 kB 0.00 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable-semver/react/cjs/react.react-server.production.min.js +4.95% 7.63 kB 8.01 kB +4.01% 3.15 kB 3.27 kB
oss-stable/react/cjs/react.react-server.production.min.js +4.94% 7.66 kB 8.04 kB +4.00% 3.17 kB 3.30 kB
facebook-www/ReactServer-prod.modern.js +4.83% 16.84 kB 17.65 kB +4.35% 4.41 kB 4.61 kB
oss-stable-semver/react/cjs/react.production.min.js +4.42% 8.66 kB 9.05 kB +3.97% 3.25 kB 3.38 kB
oss-stable/react/cjs/react.production.min.js +4.41% 8.69 kB 9.07 kB +4.00% 3.27 kB 3.40 kB
oss-experimental/react/cjs/react.production.min.js +3.97% 9.64 kB 10.02 kB +3.72% 3.57 kB 3.71 kB
oss-experimental/react/cjs/react.react-server.production.min.js +3.95% 9.70 kB 10.08 kB +3.29% 3.86 kB 3.99 kB
facebook-react-native/react/cjs/React-prod.js +3.70% 20.53 kB 21.29 kB +3.87% 5.15 kB 5.35 kB
facebook-www/React-prod.modern.js +3.69% 20.60 kB 21.36 kB +3.73% 5.15 kB 5.34 kB
facebook-react-native/react/cjs/React-profiling.js +3.67% 20.67 kB 21.43 kB +3.82% 5.16 kB 5.36 kB
facebook-www/React-prod.classic.js +3.63% 20.88 kB 21.64 kB +3.68% 5.22 kB 5.41 kB
facebook-www/React-profiling.modern.js +3.61% 21.03 kB 21.79 kB +3.69% 5.23 kB 5.43 kB
facebook-www/React-profiling.classic.js +3.56% 21.32 kB 22.08 kB +3.62% 5.30 kB 5.49 kB
oss-stable-semver/react/umd/react.profiling.min.js +3.12% 12.27 kB 12.65 kB +2.60% 4.73 kB 4.86 kB
oss-stable-semver/react/umd/react.production.min.js +3.12% 12.27 kB 12.65 kB +2.60% 4.73 kB 4.86 kB
oss-stable/react/umd/react.profiling.min.js +3.12% 12.29 kB 12.68 kB +2.65% 4.76 kB 4.88 kB
oss-stable/react/umd/react.production.min.js +3.12% 12.30 kB 12.68 kB +2.63% 4.76 kB 4.88 kB
oss-experimental/react/umd/react.profiling.min.js +2.90% 13.19 kB 13.57 kB +2.44% 5.04 kB 5.16 kB
oss-experimental/react/umd/react.production.min.js +2.90% 13.19 kB 13.57 kB +2.44% 5.04 kB 5.16 kB
facebook-react-native/react/cjs/React-dev.js +2.31% 124.24 kB 127.11 kB +1.28% 29.63 kB 30.01 kB
facebook-www/React-dev.modern.js +2.30% 124.59 kB 127.46 kB +1.29% 29.63 kB 30.01 kB
oss-stable-semver/react/cjs/react.development.js +2.30% 98.80 kB 101.07 kB +1.38% 26.56 kB 26.93 kB
oss-stable/react/cjs/react.development.js +2.30% 98.83 kB 101.10 kB +1.37% 26.60 kB 26.96 kB
facebook-www/React-dev.classic.js +2.27% 126.17 kB 129.04 kB +1.32% 30.01 kB 30.40 kB
oss-experimental/react/cjs/react.development.js +2.27% 100.14 kB 102.41 kB +1.34% 27.24 kB 27.60 kB
oss-stable-semver/react/umd/react.development.js +1.96% 121.40 kB 123.78 kB +1.17% 31.13 kB 31.50 kB
oss-stable/react/umd/react.development.js +1.96% 121.43 kB 123.81 kB +1.16% 31.16 kB 31.52 kB
oss-stable-semver/react/cjs/react.react-server.production.js +1.95% 33.80 kB 34.45 kB +1.28% 10.06 kB 10.19 kB
oss-stable/react/cjs/react.react-server.production.js +1.95% 33.82 kB 34.48 kB +1.27% 10.09 kB 10.22 kB
oss-experimental/react/umd/react.development.js +1.94% 122.74 kB 125.12 kB +1.14% 31.85 kB 32.21 kB
oss-stable-semver/react/cjs/react.production.js +1.71% 38.64 kB 39.30 kB +1.16% 10.74 kB 10.86 kB
oss-stable/react/cjs/react.production.js +1.70% 38.67 kB 39.33 kB +1.18% 10.77 kB 10.89 kB
oss-experimental/react/cjs/react.react-server.production.js +1.64% 40.20 kB 40.86 kB +1.08% 11.87 kB 11.99 kB
oss-experimental/react/cjs/react.production.js +1.59% 41.50 kB 42.16 kB +1.11% 11.51 kB 11.64 kB
facebook-www/ReactServer-dev.modern.js +1.03% 94.31 kB 95.28 kB +0.70% 22.37 kB 22.53 kB
oss-stable-semver/react/cjs/react.react-server.development.js +0.86% 76.10 kB 76.76 kB +0.63% 21.12 kB 21.26 kB
oss-stable/react/cjs/react.react-server.development.js +0.86% 76.13 kB 76.78 kB +0.64% 21.15 kB 21.29 kB
oss-experimental/react/cjs/react.react-server.development.js +0.79% 82.64 kB 83.29 kB +0.55% 23.21 kB 23.33 kB
oss-experimental/react-art/cjs/react-art.production.min.js +0.31% 97.71 kB 98.01 kB = 30.10 kB 30.07 kB
oss-stable-semver/react-art/cjs/react-art.production.min.js +0.30% 100.17 kB 100.47 kB = 30.75 kB 30.73 kB
oss-stable/react-art/cjs/react-art.production.min.js +0.30% 100.22 kB 100.52 kB = 30.78 kB 30.76 kB
oss-experimental/react-test-renderer/cjs/react-test-renderer.production.min.js +0.29% 104.49 kB 104.79 kB +0.02% 32.19 kB 32.20 kB
oss-experimental/react-test-renderer/umd/react-test-renderer.production.min.js +0.28% 104.78 kB 105.08 kB = 32.53 kB 32.53 kB
oss-stable-semver/react-test-renderer/cjs/react-test-renderer.production.min.js +0.28% 108.81 kB 109.12 kB = 33.40 kB 33.36 kB
oss-stable/react-test-renderer/cjs/react-test-renderer.production.min.js +0.28% 108.87 kB 109.17 kB = 33.43 kB 33.39 kB
oss-stable-semver/react-test-renderer/umd/react-test-renderer.production.min.js +0.27% 109.10 kB 109.40 kB = 33.77 kB 33.73 kB
oss-stable/react-test-renderer/umd/react-test-renderer.production.min.js +0.27% 109.15 kB 109.45 kB = 33.79 kB 33.76 kB
oss-experimental/react-reconciler/cjs/react-reconciler.production.min.js +0.26% 112.45 kB 112.74 kB = 34.99 kB 34.98 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.production.min.js +0.25% 114.20 kB 114.49 kB = 35.38 kB 35.36 kB
oss-stable/react-reconciler/cjs/react-reconciler.production.min.js +0.25% 114.22 kB 114.51 kB = 35.40 kB 35.39 kB
oss-experimental/react-reconciler/cjs/react-reconciler.profiling.min.js +0.24% 121.49 kB 121.79 kB = 37.32 kB 37.29 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.profiling.min.js +0.24% 123.24 kB 123.54 kB = 37.73 kB 37.72 kB
oss-stable/react-reconciler/cjs/react-reconciler.profiling.min.js +0.24% 123.27 kB 123.56 kB = 37.76 kB 37.75 kB
oss-experimental/react-art/umd/react-art.production.min.js +0.22% 134.92 kB 135.22 kB +0.05% 42.15 kB 42.17 kB
oss-stable-semver/react-art/umd/react-art.production.min.js +0.21% 137.37 kB 137.66 kB +0.05% 42.83 kB 42.85 kB
oss-stable/react-art/umd/react-art.production.min.js +0.21% 137.42 kB 137.71 kB +0.05% 42.85 kB 42.88 kB
facebook-www/ReactDOMTesting-prod.modern.js = 589.65 kB 588.44 kB = 104.94 kB 104.61 kB
facebook-www/ReactDOM-prod.classic.js = 589.44 kB 588.24 kB = 103.72 kB 103.41 kB
facebook-www/ReactDOM-prod.modern.js = 572.97 kB 571.76 kB = 100.75 kB 100.45 kB
react-native/implementations/ReactFabric-profiling.fb.js = 394.25 kB 393.18 kB = 68.59 kB 68.32 kB
react-native/implementations/ReactNativeRenderer-profiling.fb.js = 401.74 kB 400.60 kB = 69.92 kB 69.66 kB
react-native/implementations/ReactFabric-prod.fb.js = 366.97 kB 365.90 kB = 64.34 kB 64.10 kB
react-native/implementations/ReactNativeRenderer-prod.fb.js = 374.40 kB 373.25 kB = 65.62 kB 65.39 kB
facebook-www/ReactART-prod.classic.js = 365.31 kB 364.16 kB = 61.77 kB 61.57 kB
facebook-www/ReactART-prod.modern.js = 353.85 kB 352.70 kB = 59.78 kB 59.60 kB
react-native/implementations/ReactFabric-profiling.js = 329.53 kB 328.45 kB = 57.58 kB 57.34 kB
react-native/implementations/ReactFabric-prod.js = 311.02 kB 309.98 kB = 54.52 kB 54.28 kB
react-native/implementations/ReactNativeRenderer-profiling.js = 339.07 kB 337.91 kB = 59.24 kB 59.02 kB
facebook-react-native/react-test-renderer/cjs/ReactTestRenderer-profiling.js = 331.13 kB 329.98 kB = 58.09 kB 57.89 kB
react-native/implementations/ReactNativeRenderer-prod.js = 319.85 kB 318.70 kB = 56.05 kB 55.82 kB
facebook-react-native/react-test-renderer/cjs/ReactTestRenderer-prod.js = 315.33 kB 314.19 kB = 55.62 kB 55.42 kB
test_utils/ReactAllWarnings.js Deleted 65.29 kB 0.00 kB Deleted 16.34 kB 0.00 kB

Generated by 🚫 dangerJS against 0928d91

event.preventDefault();
caughtErrors.push(event.error);
}
window.addEventListener('error', errorHandler);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We don't have "act" in the TypeScript environment.

@@ -140,6 +142,6 @@ describe('ReactFlushSync (AggregateError not available)', () => {
// AggregateError is not available, React throws the first error, then
// throws the remaining errors in separate tasks.
expect(error).toBe(aahh);
expect(flushFakeMicrotasks).toThrow(nooo);
await flushFakeMicrotasks();
Copy link
Member

Choose a reason for hiding this comment

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

Should we assert somewhere that this was thrown to reportError or whatever?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So it's a bit weird about what we should be testing here. Because this is to test that something works even if aggregated errors is not supported by the platform. However, we only do aggregation at all in act() now. So we're testing act() and if you use act in an environment that doesn't support aggregated errors, then this does get dropped. Not logged somewhere else.

Which I guess is mostly if you use act in a non-JSDOM environment.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense, we should probably update the comment on 143 then

expect.objectContaining({
message: 'Boom',
}),
],
]);
expect(console.warn.mock.calls).toEqual([
Copy link
Member

Choose a reason for hiding this comment

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

We're moving some logs from console.error to console.warn right? Can we add that to the "Breaking changes" in the description?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So it used to be that we had two logs. One for the error and then one for the suffix. In my previous PR removing invokeGuardedCallback I was able to unify them into one but what this is doing is basically restoring it into two because we can't pass a component stack to reportGlobalError for uncaught but we can for caught which just goes to console.error.

It really isn't great to have the two logs, especially if the former gets called with preventDefault which used to silence the second one but we can't do that anymore.

I'm kind of inclined to say that we shouldn't have this second log at all and let uncaught not have component stacks by default and instead if you want that you implement it yourself using onUncaughtError.

Copy link
Member

Choose a reason for hiding this comment

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

I like it. But why switch from error to warn?

Would using onUncaughtError result in double logging or would we not use reportGlobalError if that's provided?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The warn is mainly about telling you to add an error boundary. It's not really so much about the component stack anymore although it includes it. Where as before it was more like two console.error as a pair. The console.warn can stand on its own.

You should really always have a root error boundary and if you don't want to you should at least implement onUncaughtError. So this shouldn't really be a case you need to hit beyond seeing the warning.

onUncaughtError takes control so this doesn't get logged then.

@rickhanlonii
Copy link
Member

rickhanlonii commented Mar 25, 2024

[WIP comment] I'm going to update this as I go through it

Making some notes as I fix the tests, it looks like these are some changes included:

Internal Tests

  • act will re-throw caught errors, so you have to add .rejects.toThrow
  • this supports single or multiple errors thrown

Single error

// TODO: why do some of my assertions require the extra array and not others?
await expect(async () => {
  await act(() => {
    root.render(<WithError />);
  });
}).rejects.toThrow(
  expect.objectContaining({
    message: "Boom",
  }),
);

Multiple errors with asymmetric matchers

await expect(async () => {
  await act(() => {
    root.render(<WithError />);
  });
}).rejects.toThrow(
  expect.objectContaining({
    errors: [
      expect.objectContaining({
        message: "Boom",
      }),
      expect.objectContaining({
        message: "Boom",
      }),
    ],
  }),
);

Multiple errors with error constructors

await expect(async () => {
  await act(() => {
    root.render(<WithError />);
  });
}).rejects.toThrow(
    // eslint-disable-next-line no-undef
    new AggregateError([
      new Error('Boom'),
      new Error('Boom'),
    ]),
);

Multiple errors with waitForThrow helper

root.render(<WithError /).
const aggregateError = await waitForThrow();
expect(aggregateError.errors.length).toBe(2);
expect(aggregateError.errors[0].message).toContain('Boom');
expect(aggregateError.errors[0].message).toContain('Boom');

Public builds

  • If an error is reported in a handler
    • It is still reported via window
    • It's no longer duplicated to console.error
  • If an error is not in an error boundary
    • the error itself is reported via window
      • Previously it was reported to console.error
    • In DEV, the addendum is reported via console.warn
      • Previously it was reported to console.error
  • If an error is in a boundary, it is still reported via console.error
  • Some legacy errors no longer have component stacks

@rickhanlonii
Copy link
Member

Things that suck about the await expect and .reject.toThrow we can improve later:

  • if you forget .rejects with rejects.toThrow jest watch mode crashes
  • having to write .rejects all the time kinda sucks, can fix with custom matcher or assertUncaught
  • having to write all these nest awaits kinda sucks, helpers like assertUncaught could help
  • asserting multiple errors in toThrow is hard, also `assertUncaught maybe?

@rickhanlonii
Copy link
Member

rickhanlonii commented Mar 26, 2024

I fixed the legacy tests but there's still two non-legacy left:

Remaining tests to fix

  • Legacy

    • ReactDOMConsoleErrorReportingLegacy-test.js
    • ReactLegacyErrorBoundaries-test.internal.js
    • ReactLegacyUpdates-test.js
    • ReactDOMLegacyFiber-test.js
    • ReactDOM-test.js
  • Non-legacy

    • ReactFresh-test.js (www)
    • useSyncExternalStoreShared-test.js (stable, www)

'Warning: React.jsx: type is invalid -- expected a string ' +
'(for built-in components) or a class/function ' +
'(for composite components) but got: null.',
{withoutStack: 1},
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why this switched from React.createElement to React.jsx?

I assume that's also why there's no component stack either?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It wasn't actually asserted before because the render threw which means the assertion wasn't run. The assertion before was asserting the wrong thing.

Copy link
Member

Choose a reason for hiding this comment

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

hahahaha awesome

}).toErrorDev(
'Expected the last optional `callback` argument to be ' +
'a function. Instead received: no.',
{withoutStack: 1},
Copy link
Member

Choose a reason for hiding this comment

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

Many of the test changes here removed the component stacks, is that expected?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These are the same. They weren't actually asserting before.

Copy link
Member

Choose a reason for hiding this comment

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

Sweet cool cool cool

Copy link
Member

@rickhanlonii rickhanlonii left a comment

Choose a reason for hiding this comment

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

LGTM

These should always be inside of DOM event listeners anyway.
This lets us report more than one error.

Not sure what to do about the React Native one.
This ensures that they can get logged properly through the regular onerror
mechanism.
Also let waitForThrow listen to global errors too.
We try to throw the first error discovered even if other errors happen later
which more closely models what used to happen when the act scope was aborted
early.
Since act now swallows the logging, we need to use a fakeAct to test these cases.

Since there's no rethrow, only logging, this case doesn't throw the original error anymore.
…rors

Such as errors logged in event handlers and onRecoverableErrors.

This is different from the public act which doesn't catch those.
@sebmarkbage sebmarkbage merged commit 6786563 into facebook:main Mar 27, 2024
37 of 38 checks passed
github-actions bot pushed a commit that referenced this pull request Mar 27, 2024
Stacked on top of #28498 for test fixes.

### Don't Rethrow

When we started React it was 1:1 setState calls a series of renders and
if they error, it errors where the setState was called. Simple. However,
then batching came and the error actually got thrown somewhere else.
With concurrent mode, it's not even possible to get setState itself to
throw anymore.

In fact, all APIs that can rethrow out of React are executed either at
the root of the scheduler or inside a DOM event handler.
If you throw inside a React.startTransition callback that's sync, then
that will bubble out of the startTransition but if you throw inside an
async callback or a useTransition we now need to handle it at the hook
site. So in 19 we need to make all React.startTransition swallow the
error (and report them to reportError).

The only one remaining that can throw is flushSync but it doesn't really
make sense for it to throw at the callsite neither because batching.
Just because something rendered in this flush doesn't mean it was
rendered due to what was just scheduled and doesn't mean that it should
abort any of the remaining code afterwards. setState is fire and forget.
It's send an instruction elsewhere, it's not part of the current
imperative code.

Error boundaries never rethrow. Since you should really always have
error boundaries, most of the time, it wouldn't rethrow anyway.

Rethrowing also actually currently drops errors on the floor since we
can only rethrow the first error, so to avoid that we'd need to call
reportError anyway. This happens in RN events.

The other issue with rethrowing is that it logs an extra console.error.
Since we're not sure that user code will actually log it anywhere we
still log it too just like we do with errors inside error boundaries
which leads all of these to log twice.
The goal of this PR is to never rethrow out of React instead, errors
outside of error boundaries get logged to reportError. Event system
errors too.

### Breaking Changes

The main thing this affects is testing where you want to inspect the
errors thrown. To make it easier to port, if you're inside `act` we
track the error into act in an aggregate error and then rethrow it at
the root of `act`. Unlike before though, if you flush synchronously
inside of act it'll still continue until the end of act before
rethrowing.

I expect most user code breakages would be to migrate from `flushSync`
to `act` if you assert on throwing.

However, in the React repo we also have `internalAct` and the
`waitForThrow` helpers. Since these have to use public production
implementations we track these using the global onerror or process
uncaughtException. Unlike regular act, includes both event handler
errors and onRecoverableError by default too. Not just render/commit
errors. So I had to account for that in our tests.

We restore logging an extra log for uncaught errors after the main log
with the component stack in it. We use `console.warn`. This is not yet
ignorable if you preventDefault to the main error event. To avoid
confusion if you don't end up logging the error to console I just added
`An error occurred`.

### Polyfill

All browsers we support really supports `reportError` but not all test
and server environments do, so I implemented a polyfill for browser and
node in `shared/reportGlobalError`. I don't love that this is included
in all builds and gets duplicated into isomorphic even though it's not
actually needed in production. Maybe in the future we can require a
polyfill for this.

### Follow Ups

In a follow up, I'll make caught vs uncaught error handling be
configurable too.

---------

Co-authored-by: Ricky Hanlon <rickhanlonii@gmail.com>

DiffTrain build for [6786563](6786563)
sebmarkbage added a commit that referenced this pull request Mar 27, 2024
Stacked on #28627.

This makes error logging configurable using these
`createRoot`/`hydrateRoot` options:

```
onUncaughtError(error: mixed, errorInfo: {componentStack?: ?string}) => void
onCaughtError(error: mixed, errorInfo: {componentStack?: ?string, errorBoundary?: ?React.Component<any, any>}) => void
onRecoverableError(error: mixed, errorInfo: {digest?: ?string, componentStack?: ?string}) => void
```

We already have the `onRecoverableError` option since before.

Overriding these can be used to implement custom error dialogs (with
access to the `componentStack`).

It can also be used to silence caught errors when testing an error
boundary or if you prefer not getting logs for caught errors that you've
already handled in an error boundary.

I currently expose the error boundary instance but I think we should
probably remove that since it doesn't make sense for non-class error
boundaries and isn't very useful anyway. It's also unclear what it
should do when an error is rethrown from one boundary to another.

Since these are public APIs now we can implement the
ReactFiberErrorDialog forks using these options at the roots of the
builds. So I unforked those files and instead passed a custom option for
the native and www builds.

To do this I had to fork the ReactDOMLegacy file into ReactDOMRootFB
which is a duplication but that will go away as soon as the FB fork is
the only legacy root.
github-actions bot pushed a commit that referenced this pull request Mar 27, 2024
Stacked on #28627.

This makes error logging configurable using these
`createRoot`/`hydrateRoot` options:

```
onUncaughtError(error: mixed, errorInfo: {componentStack?: ?string}) => void
onCaughtError(error: mixed, errorInfo: {componentStack?: ?string, errorBoundary?: ?React.Component<any, any>}) => void
onRecoverableError(error: mixed, errorInfo: {digest?: ?string, componentStack?: ?string}) => void
```

We already have the `onRecoverableError` option since before.

Overriding these can be used to implement custom error dialogs (with
access to the `componentStack`).

It can also be used to silence caught errors when testing an error
boundary or if you prefer not getting logs for caught errors that you've
already handled in an error boundary.

I currently expose the error boundary instance but I think we should
probably remove that since it doesn't make sense for non-class error
boundaries and isn't very useful anyway. It's also unclear what it
should do when an error is rethrown from one boundary to another.

Since these are public APIs now we can implement the
ReactFiberErrorDialog forks using these options at the roots of the
builds. So I unforked those files and instead passed a custom option for
the native and www builds.

To do this I had to fork the ReactDOMLegacy file into ReactDOMRootFB
which is a duplication but that will go away as soon as the FB fork is
the only legacy root.

DiffTrain build for [a053716](a053716)
EdisonVan pushed a commit to EdisonVan/react that referenced this pull request Apr 15, 2024
Stacked on top of facebook#28498 for test fixes.

### Don't Rethrow

When we started React it was 1:1 setState calls a series of renders and
if they error, it errors where the setState was called. Simple. However,
then batching came and the error actually got thrown somewhere else.
With concurrent mode, it's not even possible to get setState itself to
throw anymore.

In fact, all APIs that can rethrow out of React are executed either at
the root of the scheduler or inside a DOM event handler.
If you throw inside a React.startTransition callback that's sync, then
that will bubble out of the startTransition but if you throw inside an
async callback or a useTransition we now need to handle it at the hook
site. So in 19 we need to make all React.startTransition swallow the
error (and report them to reportError).

The only one remaining that can throw is flushSync but it doesn't really
make sense for it to throw at the callsite neither because batching.
Just because something rendered in this flush doesn't mean it was
rendered due to what was just scheduled and doesn't mean that it should
abort any of the remaining code afterwards. setState is fire and forget.
It's send an instruction elsewhere, it's not part of the current
imperative code.

Error boundaries never rethrow. Since you should really always have
error boundaries, most of the time, it wouldn't rethrow anyway.

Rethrowing also actually currently drops errors on the floor since we
can only rethrow the first error, so to avoid that we'd need to call
reportError anyway. This happens in RN events.

The other issue with rethrowing is that it logs an extra console.error.
Since we're not sure that user code will actually log it anywhere we
still log it too just like we do with errors inside error boundaries
which leads all of these to log twice.
The goal of this PR is to never rethrow out of React instead, errors
outside of error boundaries get logged to reportError. Event system
errors too.

### Breaking Changes

The main thing this affects is testing where you want to inspect the
errors thrown. To make it easier to port, if you're inside `act` we
track the error into act in an aggregate error and then rethrow it at
the root of `act`. Unlike before though, if you flush synchronously
inside of act it'll still continue until the end of act before
rethrowing.

I expect most user code breakages would be to migrate from `flushSync`
to `act` if you assert on throwing.

However, in the React repo we also have `internalAct` and the
`waitForThrow` helpers. Since these have to use public production
implementations we track these using the global onerror or process
uncaughtException. Unlike regular act, includes both event handler
errors and onRecoverableError by default too. Not just render/commit
errors. So I had to account for that in our tests.

We restore logging an extra log for uncaught errors after the main log
with the component stack in it. We use `console.warn`. This is not yet
ignorable if you preventDefault to the main error event. To avoid
confusion if you don't end up logging the error to console I just added
`An error occurred`.

### Polyfill

All browsers we support really supports `reportError` but not all test
and server environments do, so I implemented a polyfill for browser and
node in `shared/reportGlobalError`. I don't love that this is included
in all builds and gets duplicated into isomorphic even though it's not
actually needed in production. Maybe in the future we can require a
polyfill for this.

### Follow Ups

In a follow up, I'll make caught vs uncaught error handling be
configurable too.

---------

Co-authored-by: Ricky Hanlon <rickhanlonii@gmail.com>
EdisonVan pushed a commit to EdisonVan/react that referenced this pull request Apr 15, 2024
Stacked on facebook#28627.

This makes error logging configurable using these
`createRoot`/`hydrateRoot` options:

```
onUncaughtError(error: mixed, errorInfo: {componentStack?: ?string}) => void
onCaughtError(error: mixed, errorInfo: {componentStack?: ?string, errorBoundary?: ?React.Component<any, any>}) => void
onRecoverableError(error: mixed, errorInfo: {digest?: ?string, componentStack?: ?string}) => void
```

We already have the `onRecoverableError` option since before.

Overriding these can be used to implement custom error dialogs (with
access to the `componentStack`).

It can also be used to silence caught errors when testing an error
boundary or if you prefer not getting logs for caught errors that you've
already handled in an error boundary.

I currently expose the error boundary instance but I think we should
probably remove that since it doesn't make sense for non-class error
boundaries and isn't very useful anyway. It's also unclear what it
should do when an error is rethrown from one boundary to another.

Since these are public APIs now we can implement the
ReactFiberErrorDialog forks using these options at the roots of the
builds. So I unforked those files and instead passed a custom option for
the native and www builds.

To do this I had to fork the ReactDOMLegacy file into ReactDOMRootFB
which is a duplication but that will go away as soon as the FB fork is
the only legacy root.
bigfootjon pushed a commit that referenced this pull request Apr 18, 2024
Stacked on top of #28498 for test fixes.

### Don't Rethrow

When we started React it was 1:1 setState calls a series of renders and
if they error, it errors where the setState was called. Simple. However,
then batching came and the error actually got thrown somewhere else.
With concurrent mode, it's not even possible to get setState itself to
throw anymore.

In fact, all APIs that can rethrow out of React are executed either at
the root of the scheduler or inside a DOM event handler.
If you throw inside a React.startTransition callback that's sync, then
that will bubble out of the startTransition but if you throw inside an
async callback or a useTransition we now need to handle it at the hook
site. So in 19 we need to make all React.startTransition swallow the
error (and report them to reportError).

The only one remaining that can throw is flushSync but it doesn't really
make sense for it to throw at the callsite neither because batching.
Just because something rendered in this flush doesn't mean it was
rendered due to what was just scheduled and doesn't mean that it should
abort any of the remaining code afterwards. setState is fire and forget.
It's send an instruction elsewhere, it's not part of the current
imperative code.

Error boundaries never rethrow. Since you should really always have
error boundaries, most of the time, it wouldn't rethrow anyway.

Rethrowing also actually currently drops errors on the floor since we
can only rethrow the first error, so to avoid that we'd need to call
reportError anyway. This happens in RN events.

The other issue with rethrowing is that it logs an extra console.error.
Since we're not sure that user code will actually log it anywhere we
still log it too just like we do with errors inside error boundaries
which leads all of these to log twice.
The goal of this PR is to never rethrow out of React instead, errors
outside of error boundaries get logged to reportError. Event system
errors too.

### Breaking Changes

The main thing this affects is testing where you want to inspect the
errors thrown. To make it easier to port, if you're inside `act` we
track the error into act in an aggregate error and then rethrow it at
the root of `act`. Unlike before though, if you flush synchronously
inside of act it'll still continue until the end of act before
rethrowing.

I expect most user code breakages would be to migrate from `flushSync`
to `act` if you assert on throwing.

However, in the React repo we also have `internalAct` and the
`waitForThrow` helpers. Since these have to use public production
implementations we track these using the global onerror or process
uncaughtException. Unlike regular act, includes both event handler
errors and onRecoverableError by default too. Not just render/commit
errors. So I had to account for that in our tests.

We restore logging an extra log for uncaught errors after the main log
with the component stack in it. We use `console.warn`. This is not yet
ignorable if you preventDefault to the main error event. To avoid
confusion if you don't end up logging the error to console I just added
`An error occurred`.

### Polyfill

All browsers we support really supports `reportError` but not all test
and server environments do, so I implemented a polyfill for browser and
node in `shared/reportGlobalError`. I don't love that this is included
in all builds and gets duplicated into isomorphic even though it's not
actually needed in production. Maybe in the future we can require a
polyfill for this.

### Follow Ups

In a follow up, I'll make caught vs uncaught error handling be
configurable too.

---------

Co-authored-by: Ricky Hanlon <rickhanlonii@gmail.com>

DiffTrain build for commit 6786563.
bigfootjon pushed a commit that referenced this pull request Apr 18, 2024
Stacked on #28627.

This makes error logging configurable using these
`createRoot`/`hydrateRoot` options:

```
onUncaughtError(error: mixed, errorInfo: {componentStack?: ?string}) => void
onCaughtError(error: mixed, errorInfo: {componentStack?: ?string, errorBoundary?: ?React.Component<any, any>}) => void
onRecoverableError(error: mixed, errorInfo: {digest?: ?string, componentStack?: ?string}) => void
```

We already have the `onRecoverableError` option since before.

Overriding these can be used to implement custom error dialogs (with
access to the `componentStack`).

It can also be used to silence caught errors when testing an error
boundary or if you prefer not getting logs for caught errors that you've
already handled in an error boundary.

I currently expose the error boundary instance but I think we should
probably remove that since it doesn't make sense for non-class error
boundaries and isn't very useful anyway. It's also unclear what it
should do when an error is rethrown from one boundary to another.

Since these are public APIs now we can implement the
ReactFiberErrorDialog forks using these options at the roots of the
builds. So I unforked those files and instead passed a custom option for
the native and www builds.

To do this I had to fork the ReactDOMLegacy file into ReactDOMRootFB
which is a duplication but that will go away as soon as the FB fork is
the only legacy root.

DiffTrain build for commit a053716.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team React 19
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants