Skip to content

Sentry Error Boundary with Offline Queue and Network-Aware Retry #53

Description

@JamesEjembi

Problem Statement / Feature Objective

Uncaught React errors, WebSocket connection drops, and API 5xx responses are reported to Sentry but lost when the device is offline. Errors also trigger repetitive alerts for transient failures. A Sentry error boundary must queue errors locally during offline periods and replays them on connectivity restore, with network-aware deduplication and backoff.


Technical Invariants & Bounds

  • Offline queue capacity: 500 error records before LRU eviction
  • Retry backoff: exponential, starting at 1 s, doubling, max 60 s, reset after 10 min of uptime
  • Deduplication window: same error + same component + same hash within 5 seconds → merge with counter
  • Max retry attempts per error: 5; after that, drop silently (no infinite retry)
  • Queue persisted in IndexedDB under store sentry/offlineQueue

Codebase Navigation Guide

  • src/lib/sentry/sentryClient.ts — current Sentry.init() and captureException wrapper
  • src/components/error/AppErrorBoundary.tsx — existing React error boundary
  • src/hooks/useNetworkStatus.ts — existing hook exposing navigator.onLine + online/offline events
  • src/lib/storage/idb.ts — IndexedDB helper for persistence
  • src/pages/_app.tsx (or equivalent root) — where Sentry.init is called

Implementation Blueprint

  1. Extend src/lib/sentry/sentryClient.ts with enqueueError(error, context) that checks navigator.onLine. If offline, stores the error in IndexedDB under sentry/offlineQueue with { timestamp, errorHash, componentStack, count: 1 }. If online, calls Sentry.captureException immediately.
  2. Create processOfflineQueue() function that reads all queued errors, attempts Sentry.captureException for each, and deletes them on success. On failure, increments retry count and re-stores with updated backoff timestamp.
  3. In AppErrorBoundary.tsx, override componentDidCatch to call enqueueError instead of directly calling Sentry. Render a fallback UI with a "Retry" button and an indicator of queued errors.
  4. Subscribe to useNetworkStatuss

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions