# Error Boundary Pattern

## Objective

Contain a render failure to the part of the screen that broke.

Failure containment placed at deliberate points in the interface, each with its own fallback and recovery path.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Choose the containment points deliberately: the route level, each independently useful region of a page, and any widget rendering data the app does not control. Everything between those points inherits its nearest boundary.
2. Give each boundary a fallback sized and shaped like the thing it replaced, so a failed sidebar widget leaves a widget-shaped message rather than collapsing the layout around it.
3. Offer a recovery that is proportionate to the failure — retry this region, go back to the previous screen, or reload — and never leave a dead panel with no way forward.
4. Send the failure to the app's existing error reporting with enough context to locate it: the boundary's name, the route, and the record type. Not the record contents.
5. Rendering failures are this feature's concern; a request that returns an error is Async Boundary Component's concern. Do not route failed responses through here, because a handled server error is not an exceptional condition and does not deserve a crash fallback.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- A single boundary wrapped around the whole application turns every small fault into a blank page. Place boundaries low enough that the navigation, the header, and the unaffected regions all survive.
- A boundary that has caught an error stays caught until it is told otherwise, so it must reset when the route or the record it was rendering changes. Otherwise the user navigates somewhere healthy and still sees the old failure.
- Reports must carry enough to debug and nothing more. Serialising the failed component's props will send record contents, tokens, and personal data to a third-party service.
- Navigation must keep working. If the fallback replaces the region that contained the only way out, the user is stranded and their only option is the browser's back button.
- An error thrown inside the fallback itself will loop. Keep fallbacks trivial: static text, one or two controls, no data access.
- Failures during an initial server render behave differently from failures after the page is interactive, and the boundary must produce a sensible result in both rather than only the one that was tested.
- Repeated automatic retries of a deterministic failure will spin. Cap the attempts and then require an explicit action from the user.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Boundaries exist at the route level and around each independently useful region, not only at the application root.
- [ ] A caught failure leaves navigation and the rest of the page usable.
- [ ] Each fallback offers a recovery path and occupies roughly the space of the content it replaced.
- [ ] A route or record change resets a boundary that had previously caught an error.
- [ ] Reports include boundary, route, and context, and contain no props, record data, or credentials.
- [ ] Fallbacks cannot themselves throw, and automatic retries are capped.
- [ ] Failed requests are handled by Async Boundary Component and do not surface as crash fallbacks.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
