feat(shared): add ReactErrorAnalyzer for common error pattern recogni…
Summary
Motivation: Currently, React error boundaries catch errors but provide minimal context, making debugging difficult for common patterns. Furthermore, stack traces lack source map resolution in the DevTools console. This PR addresses Issue #36719 by explicitly identifying common error patterns and providing immediate, actionable fixes to improve the developer experience.
Changes made:
- Error Pattern Analyzer (
packages/shared/ReactErrorAnalyzer.js): Created a utility to parse error strings and match them against common React pitfalls (e.g., null dereferences, invalid hook calls). - Reconciler Integration (
packages/react-reconciler/src/ReactFiberErrorLogger.js): Injected the analyzer intodefaultOnCaughtErroranddefaultOnUncaughtErrorstrictly during__DEV__. It usesconsole.groupCollapsedto output highly visible recovery badges without cluttering the console. - DevTools UI (
packages/react-devtools-shared/src/devtools/views/ErrorBoundary/ErrorBoundary.js): Integrated the analyzer to display a " Suggested Fix" card directly inside the DevTools Error Boundary inspection view. - Source Map Resolution (
packages/react-devtools-shared/src/backend/console.js): Intercepted component stack traces in the DevTools shell console overrides to asynchronously fetch and resolve.mapfiles when available.
Fixes #36719
How did you test this change?
To ensure these changes are safe and do not impact production bundles, I verified the following:
1. Automated Checks (Passed):
- Ran
yarn prettierto ensure code formatting aligns with the repo standards. - Ran
yarn lintto verify no linting regressions. - Ran
yarn flowto ensure the newReactErrorAnalyzertypes (string | null | void) correctly interface with themixederror types in the Reconciler. - Ran
yarn testandyarn test --prodto ensure the Reconciler and DevTools test suites pass successfully.
2. Manual Verification & UI Testing:
- Zero Production Impact: Verified that the Reconciler changes are strictly wrapped in
if (__DEV__)blocks, ensuringReactErrorAnalyzerdoes not inflate production bundle sizes. - Console Output: Triggered a "Cannot read properties of null" error and an "Invalid hook call" error in a local test app. Verified that
console.groupCollapsedsuccessfully renders the cyan/green " React Recovery Suggestion" badge alongside the standard React error logs. - DevTools UI: Opened React DevTools and inspected an element wrapped in an Error Boundary that had caught an error. Verified the new suggested fix block renders cleanly above the component stack without breaking the Suspense fallbacks.
- Source Maps: Verified that the console interceptor successfully appends the
[Source Map Available]badge to stack trace lines when a valid.mapfile is fetchable.
Hi @mehtahet619!
Thank you for your pull request and welcome to our community.
Action Required
In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.
Process
In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.
Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.
If you have received this in error or have any questions, please contact us at [email protected]. Thanks!
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!