Skip to content

fix(theme-common): report recoverable storage errors via reportError() (AI-assisted)#12021

Open
LeSingh1 wants to merge 1 commit into
facebook:mainfrom
LeSingh1:report-recoverable-storage-errors
Open

fix(theme-common): report recoverable storage errors via reportError() (AI-assisted)#12021
LeSingh1 wants to merge 1 commit into
facebook:mainfrom
LeSingh1:report-recoverable-storage-errors

Conversation

@LeSingh1
Copy link
Copy Markdown

Pre-flight checklist

  • I have read the Contributing Guidelines on pull requests.
  • If this is a code change: I have written unit tests and/or added dogfooding pages to fully verify the new behavior.
  • If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

Closes #6747.

Today, when Docusaurus's createStorageSlot (used for color mode, announcement bar dismissal, etc.) hits a failing localStorage/sessionStorage operation — for example in an iframe, an incognito session, or under strict browser privacy settings — it logs to console.error and continues. Site owners using window.onerror, window.addEventListener('error', ...), or third-party error reporting may not see these failures today, even though they can be useful diagnostics for real browser/runtime conditions.

The HTML spec's reportError() API (Baseline since 2022) is purpose-built for this: it dispatches an ErrorEvent to global listeners just like an uncaught exception would, without actually throwing. This makes the error observable to host-page tooling while keeping the recovery path unchanged.

What this PR does

  • Adds a tiny reportRecoverableError(error) helper in @docusaurus/theme-common that prefers globalThis.reportError and falls back to console.error when it's unavailable (older browsers, some test runners).
  • Migrates the four console.error call sites inside createStorageSlot (get / set / del / listen) to use the helper. Original messages are preserved as the new Error's message; the underlying browser exception is preserved via Error.cause (a pattern already used elsewhere in the repo — see vitest.config.ts's snapshotErrorCause.ts serializer).
  • Leaves intentional console.error calls untouched: deprecation warnings in colorMode.tsx, locale diagnostics in usePluralForm.ts, and CLI/server logging. Those are not recoverable runtime errors.

Scope was kept deliberately small so the helper and approach can be reviewed in isolation; follow-up PRs can migrate additional call sites incrementally if maintainers are happy with the pattern.

Test plan

  • yarn test packages/docusaurus-theme-common/src/utils/__tests__/errorUtils.test.ts — 2/2 pass, covers both the reportError-present and fallback paths.
  • yarn test packages/docusaurus-theme-common — 108/108 pass, no regressions in existing storage / color mode behavior.
  • yarn tsc --noEmit in packages/docusaurus-theme-common — clean.
  • yarn lint:js — no new errors or warnings on the changed files.

Notes

This change is AI-assisted, per AGENTS.md. The fallback to console.error preserves the existing behavior for older browsers and test environments without reportError.

@meta-cla meta-cla Bot added the CLA Signed Signed Facebook CLA label May 14, 2026
@netlify
Copy link
Copy Markdown

netlify Bot commented May 14, 2026

[V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit 2c56c23
🔍 Latest deploy log https://app.netlify.com/projects/docusaurus-2/deploys/6a060efe6e091a0009c9588d
😎 Deploy Preview https://deploy-preview-12021--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

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

Labels

CLA Signed Signed Facebook CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use reportError() API

1 participant