Skip to content

Add an opt-in error boundary around the root component - #60380

Draft
ide wants to merge 1 commit into
react:mainfrom
expo:@ide/surface-error-boundary
Draft

ide wants to merge 1 commit into
react:mainfrom
expo:@ide/surface-error-boundary

Conversation

@ide

@ide ide commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Apps can enable the JavaScript-only enableRootErrorBoundary flag at startup through ReactNativeFeatureFlags.override from react-native/react-private-interface. This wraps the root component in an error boundary. If an error from rendering, a lifecycle method, or an effect reaches that boundary in a release build, React Native reports it as non-fatal and keeps the app running. The failed surface stays blank until the boundary is remounted.

The flag is a per-app policy, and there is no plan to turn it on for every app. Both its default value and its expectedReleaseValue are false, so a change to its ossReleaseStage does not turn the boundary on in apps that did not opt in. Its purpose is operational because it is meant to stay; only experimentation flags carry a dateAdded that is used to prioritize their cleanup.

With the flag off, renderApplication renders the root component directly and React Native reports uncaught React errors as fatal. With the flag on, app error boundaries still handle errors within their own trees first, and WrapperComponent stays outside the root boundary.

Changelog:

[GENERAL] [ADDED] - Add an opt-in root error boundary that leaves a failed surface blank and reports the error as non-fatal.

Test Plan:

yarn jest --runInBand --runTestsByPath packages/react-native/Libraries/ReactNative/__tests__/renderApplication-test.js
yarn jest --runInBand packages/react-native/Libraries/ReactNative --testPathIgnorePatterns='/renderApplication-test\.js$'
yarn flow-check
yarn lint
yarn format-check-javascript
yarn featureflags --update

All commands pass, and yarn featureflags --update leaves the generated files unchanged. With the flag on, the focused test checks that the production container's root View is empty, and that the onCaughtError handler renderApplication passes to ReactFabric.render calls ExceptionsManager.handleException(error, false). The React test renderer does not accept error handlers, so the test calls that handler with the error itself. With the flag off, the test checks that the error stays uncaught. Each assertion fails when its behavior is removed: a non-empty boundary fallback, a fatal report from onCaughtError, onUncaughtError passed in its place, and the boundary forced on or off. The release reporting behavior was verified in the native sources. Device behavior was outside the test run.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 29, 2026
@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Nov 29, 2026
@ide
ide force-pushed the @ide/surface-error-boundary branch from 4f0d3d0 to 4d1c761 Compare November 29, 2026 04:51
@ide ide changed the title Catch root component render errors in an error boundary Catch errors under the root component in an error boundary Nov 29, 2026
@ide
ide force-pushed the @ide/surface-error-boundary branch 2 times, most recently from 2b4bf83 to ee96497 Compare November 29, 2026 12:02
@ide
ide marked this pull request as ready for review November 29, 2026 13:49
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 29, 2026
@javache
javache requested a review from rubennorte December 3, 2026 14:29
@javache

javache commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

I think we want to do a more general change here which should go through an RFC, to stop considering all uncaught errors (regardless of whether they come from React or not) as fatal.

Comment on lines +57 to +59
render(): React.Node {
return this.state.hasError ? null : this.props.children;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This means that after an error, the app just blanks out, which is worse IMO than crashing.

@ide

ide commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Makes a lot of sense to me, you mentioned that before too. I'll set this as a draft.

@ide
ide marked this pull request as draft December 3, 2026 14:36

@chrfalch chrfalch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This change turns every render error under the root component from fatal into non-fatal, for every app, with no way to opt out. In development nothing visible changes, because LogBox already shows component errors as fatal. In a release build the effect is large. Today a fatal error makes iOS reload the bundle and then crash, and makes Android throw a JavascriptException, so crash reporters record it and the user gets a fresh start. With this boundary, the root view stays empty for the rest of the session: iOS only calls the soft-exception delegate, and Android only writes the error to the log. I would gate this behind a feature flag or a renderApplication option, and mark it as a breaking change in the changelog. The test also does not show that the boundary rendered nothing (see the inline comment).

@ide
ide force-pushed the @ide/surface-error-boundary branch from ee96497 to 7585b38 Compare December 5, 2026 08:04
@ide ide changed the title Catch errors under the root component in an error boundary Add an opt-in error boundary around the root component Dec 5, 2026
@ide

ide commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

The root error boundary now sits behind enableRootErrorBoundary, which defaults to off. Apps that opt in choose a blank surface and a non-fatal report when a render error reaches the boundary. The test checks that the production container's root View has no children and that React reports the error as caught with the flag on. A separate test checks that the error stays uncaught by default.

@chrfalch chrfalch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The boundary is now behind a flag that is off by default, and the test checks that the root View stays empty. Two small points remain: the flag's metadata says it is expected to become the default, and the test checks React's own log message instead of the non-fatal report that React Native sends.

Apps can enable `enableRootErrorBoundary` at startup to wrap the root component in an error boundary. If a React error reaches that boundary in a release build, the failed surface stays blank until the boundary is remounted, and React Native reports the error as non-fatal. The flag is an operational, per-app policy: both its default value and its expected release value are off, so uncaught React errors stay fatal in every app that does not opt in.

`yarn jest --runInBand --runTestsByPath packages/react-native/Libraries/ReactNative/__tests__/renderApplication-test.js` verifies the empty root view, the non-fatal report from the caught-error handler that `renderApplication` passes to Fabric, and the default uncaught path. Each assertion fails when its behavior is removed. The remaining `Libraries/ReactNative` Jest tests, `yarn flow-check`, `yarn lint`, and `yarn format-check-javascript` pass. `yarn featureflags --update` leaves the generated files unchanged.

Signed-off-by: James Ide <ide@expo.dev>
@ide
ide force-pushed the @ide/surface-error-boundary branch from 7585b38 to cf12d40 Compare December 5, 2026 09:46
@ide

ide commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, both points are addressed in the updated commit. enableRootErrorBoundary is now an operational flag with expectedReleaseValue: false, because the boundary is a per-app policy that we do not plan to turn on for every app. The test now checks React Native's non-fatal report instead of React's log message, and the PR text says plainly that the flag is a per-app policy.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants