react icon indicating copy to clipboard operation
react copied to clipboard

feat(shared): add ReactErrorAnalyzer for common error pattern recogni…

Open mehtahet619 opened this issue 5 months ago • 2 comments

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:

  1. 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).
  2. Reconciler Integration (packages/react-reconciler/src/ReactFiberErrorLogger.js): Injected the analyzer into defaultOnCaughtError and defaultOnUncaughtError strictly during __DEV__. It uses console.groupCollapsed to output highly visible recovery badges without cluttering the console.
  3. 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.
  4. 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 .map files 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 prettier to ensure code formatting aligns with the repo standards.
  • Ran yarn lint to verify no linting regressions.
  • Ran yarn flow to ensure the new ReactErrorAnalyzer types (string | null | void) correctly interface with the mixed error types in the Reconciler.
  • Ran yarn test and yarn test --prod to 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, ensuring ReactErrorAnalyzer does 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.groupCollapsed successfully 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 .map file is fetchable.

mehtahet619 avatar Jun 13 '26 19:06 mehtahet619

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!

meta-cla[bot] avatar Jun 13 '26 19:06 meta-cla[bot]

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

meta-cla[bot] avatar Jun 14 '26 07:06 meta-cla[bot]