为React应用实现错误边界
按页面和组件划分故障隔离范围,实现降级界面、重试恢复及错误上报,并补充测试验证。
完整提示词
<context>
You are a React engineer containing failures so one broken component does not blank the whole app. What experienced React developers know about error boundaries:
- A boundary catches errors thrown while rendering, in lifecycle methods and in constructors of the components below it. It does not catch errors in event handlers, in async code (promises, timeouts), during server rendering, or in the boundary itself. Handle event and async errors with try/catch and state, or push them into the nearest boundary (for example `showBoundary` from `react-error-boundary`).
- Boundaries are class components; `react-error-boundary` is the usual wrapper if the project allows it.
- Granularity: a root boundary as the last resort, one per route or page, and one around each independent widget (chart, sidebar, comments) so the rest keeps working. Do not wrap every small component.
- Reset: give the fallback a retry that remounts the subtree, and reset automatically when the route or key inputs change (`resetKeys`). With TanStack Query, combine with `QueryErrorResetBoundary` so retry refetches.
- Report each caught error once to the error tracker with the component stack. React 19 also offers `onCaughtError` and `onUncaughtError` on the root.
- Next.js App Router: `error.tsx` (a client component receiving `error` and `reset`) catches errors in its segment's children but not in that segment's own `layout`; `global-error.tsx` handles root layout errors and must render its own `html` and `body`; `not-found.tsx` handles `notFound()`. In production, server component error messages are replaced by a digest, so show a generic message and log the digest.
- Fallbacks are calm and useful: what failed, a retry, a way to go on. Never show stack traces to users.
- In development the error overlay still appears for caught errors; test fallbacks in a production build or tests.
</context>
<task>
Add error boundaries to this app:
{{app_context}}
1. Read the root, layouts, routes, data fetching, existing error handling and error reporting setup.
2. Write the boundary map: where each boundary sits, what its fallback shows, and how it resets.
3. List the errors boundaries will not catch in this app (event handlers, async code, server rendering) and how each is handled.
4. Implement the boundaries (or Next.js error files), the fallbacks, reset and retry, reporting to the error tracker with deduplication, and the handling for uncaught classes of errors.
5. Write tests: a widget that throws shows its fallback while the rest renders, retry recovers, route change resets, an event-handler error reaches the boundary through the chosen mechanism, the report is sent once.
6. Run the tests, type checks and a production build. Fix what fails.
</task>
<constraints>
- Never swallow errors without reporting them.
- Never show stack traces or raw error messages from the server to users.
- Do not add packages the project does not already use without saying why.
{{> guardrails/scope-discipline}}
{{> guardrails/investigate-before-answering}}
{{> guardrails/verify-before-done}}
</constraints>
<output_format>
## Boundary map
A table: Boundary | Wraps | Fallback | Reset.
## Not caught by boundaries
One line per error source and how it is handled.
## Changes
One line per file: `path`, what changed.
## Tests
One line per test and the case it covers.
## Verification
Commands run and their actual results.
</output_format>可替换内容
- {{app_context}}
- {{> guardrails/scope-discipline}}
- {{> guardrails/investigate-before-answering}}
- {{> guardrails/verify-before-done}}
分类与标签
编程开发、React、错误边界、故障隔离、重试恢复、前端测试