Skip to content

feat(widget): classify errors and show only the actions that can help #89

Closed

Description

@Ardnived

Summary

Both error fallbacks render a fixed set of buttons regardless of what actually failed, so most of what a viewer sees on an error is noise or a dead end. DrawerErrorFallback (src/views/shared.tsx:346-374) always shows Try again + See nearby events + Report an issue; the app-level ErrorFallback (src/components/molecules/Fallbacks/Fallbacks.tsx:29-56) always shows only Report an issue. Neither asks whether the action can help — "Try again" is offered for a region that has no events (a retry is guaranteed to fail identically), "See nearby events" for a dropped connection (that search will fail too), and "Report an issue" for the viewer's own offline state, which is not something the team can fix.

The message is wrong-shaped too. errorMessage(error) renders the raw thrown developer string — Region has no events: pune, SahajCloud request returned no data: /events/geojson, Not an event: /india/register — untranslated, to a viewer, inside someone else's host page.

This ticket settles the whole approach: classify the failure, derive the copy and the actions from that classification, and represent every resulting state in the Ladle view stories (which today preview a fallback the drawer never actually renders).

Current failure taxonomy

Every value that can reach an error boundary today:

Thrown at Value What it means
sdk.request (non-2xx) PayloadSDKError + status 401/403 bad key, 404 gone, 5xx server
native fetch TypeError: Failed to fetch offline / CORS / CSP
src/config/api/client.ts:76 SahajCloud request returned no data: … server returned nothing (payloadcms/payload#14495)
src/config/api/fetch.ts:159, :341 Region not found: … dead or mistyped region URL
src/config/api/fetch.ts:348 Region has no events: … deliberate 404 of a real region
src/config/api/fetch.ts:575 Not authenticated as an Atlas client embed misconfigured
src/views/shared.tsx:319 Not an event: … hand-typed non-event path
any fetcher's .parse() ZodError SahajCloud contract drift

Proposed changes

1. A classifyError seam

Add it beside errorMessage in src/lib/report.ts, under the same never-throws discipline that helper already documents — it runs inside an error fallback, where a throw is unrecoverable and would blank the widget on the host page. It narrows an unknown to a kind, and each kind carries its own action policy:

kind matched by localized message Try again See nearby Report an issue
offline TypeError from fetch, navigator.onLine === false "You appear to be offline." yes — —
server 5xx, …returned no data "Our servers are having trouble right now." yes — secondary
not-found 404, Region not found:, Not an event: "We couldn't find that page." — yes —
config 401/403, Not authenticated as an Atlas client "This Atlas embed isn't set up correctly." — — yes
contract ZodError generic — — yes
unknown anything else generic yes — yes

offline deliberately suppresses Report an issue. Connectivity is not something the team can act on, and the report POST (#80) needs the same network that just failed.

2. Localized copy replaces the raw string

The thrown developer message stops being the headline and survives only as report context — it already rides to openReport(description) and into ReportContext.error, and that is where it belongs. New common.error.* keys must be added across every locale via pnpm i18n:add, not just en; short common.json UI labels in this repo have a habit of shipping en-only and leaning on the fallback.

3. Region-with-no-events becomes an empty state, not an error

src/config/api/fetch.ts:348 throws Region has no events so the boundary renders an error page. That is the one case where no button makes sense — retrying can't change it and it isn't a wrong turn. Stop throwing and render the existing EmptyEventList (src/views/shared.tsx:381) instead, which already has no buttons and which the code notes is currently "unreachable in the running app" precisely because of that throw.

Check what else depended on the 404 before removing it: the throw is deliberate ("a 0-event region isn't a destination", mirroring getCountries hiding 0-event countries), and src/config/api/fetch.test.ts:317 asserts it. Update that test to assert the new contract rather than deleting it, and confirm no canonical-URL or map-framing behaviour keyed off the throw.

4. Unify the two fallbacks on one policy

Both fallbacks should render from the same policy rather than each hard-coding a button list. The app-level boundary at src/App.tsx:70 currently can't retry at all — it passes no onReset, and ErrorFallback doesn't take resetErrorBoundary — so it needs that wiring to honour offline and server.

5. Verify "Try again" actually refetches

Neither boundary is wrapped in TanStack Query's QueryErrorResetBoundary, so resetErrorBoundary may hand the reset subtree a query that is still in its error state and re-throw immediately. Prove the retry path end-to-end and wire the reset properly if it doesn't hold — a retry button that visibly does nothing is worse than no button at all.

6. Ladle

  • src/views/story-harness.tsx:99 wraps view stories in the app-level ErrorFallback, but the real drawer stack wraps its views in DrawerErrorFallback (src/views/DrawerStack/DrawerStack.tsx:354). The stories preview a fallback the drawer never shows — fix the harness to mirror the app.
  • Give ViewHarness a throws?: unknown escape so any view story can be driven into an error state (the harness seeds a QueryClient synchronously and there's no public API for seeding a rejected query, so a thrower inside the boundary is the honest route).
  • Add a story enumerating every kind at drawer width, and extend src/components/molecules/Fallbacks/Fallbacks.stories.tsx to match.
  • Add the realistic kind to the views where it's reachable: EventView → not-found, SearchView → offline, RegionView → the new empty state.

Acceptance criteria

  • classifyError lives beside errorMessage in src/lib/report.ts, returns one of offline | server | not-found | config | contract | unknown, and never throws for any input (hostile values covered by unit tests, as errorMessage already is in src/lib/report.test.ts)
  • Each kind renders only the buttons in the policy table above — no fallback hard-codes a button list
  • offline renders Try again only; no See nearby events, no Report an issue
  • not-found renders See nearby events only; no Try again
  • config and contract render Report an issue only
  • No raw thrown developer string reaches the screen; each kind shows localized copy, and the thrown message still reaches openReport / ReportContext.error unchanged
  • New common.error.* keys exist in every public/locales/<lng>/common.json, added via pnpm i18n:add
  • A region with 0 events renders EmptyEventList with no action buttons instead of an error page; src/config/api/fetch.test.ts:317 is updated to the new contract rather than dropped
  • The app-level boundary (src/App.tsx:70) can retry, and Try again is verified to actually refetch (not re-throw the cached query error) in the running widget
  • ViewHarness wraps stories in DrawerErrorFallback, matching DrawerStack.tsx:354
  • A Ladle story renders every error kind at drawer width, and Fallbacks.stories.tsx covers the app-level fallback the same way
  • EventView, SearchView and RegionView stories each expose their reachable error/empty case
  • pnpm lint && pnpm typecheck && pnpm test:run pass; pnpm ladle:build succeeds

Files affected

  • src/lib/report.ts, src/lib/report.test.ts — the classifier + its never-throws coverage
  • src/components/molecules/Fallbacks/Fallbacks.tsx — app-level fallback, retry wiring
  • src/views/shared.tsx — DrawerErrorFallback, EmptyEventList
  • src/App.tsx — boundary onReset
  • src/config/api/fetch.ts, src/config/api/fetch.test.ts — drop the 0-event throw
  • src/views/story-harness.tsx — DrawerErrorFallback + a throws escape
  • src/components/molecules/Fallbacks/Fallbacks.stories.tsx, src/views/{EventView,SearchView,RegionView}/*.stories.tsx
  • public/locales/*/common.json

References

Activity

  1. 10 remaining items

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Hold Until

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions