# Add Error Boundaries to a React Router App

Yes. An agent adds route-level ErrorBoundary exports, a server handleError hook and a real 404 route to your React Router 7 app, with tests, in a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-error-boundaries/react-router/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads what the framework already gives the app before adding a boundary, because React Router 7 renders the nearest `ErrorBoundary` export and the template ships one at the root. In an app shaped like the `default` template in remix-run/react-router-templates, `package.json` depends on `react-router`, `@react-router/node` and `@react-router/serve` with `react 19.2`, builds with `vite 8` and `@tailwindcss/vite`, and runs `react-router typegen && tsc` as its `typecheck` script. `react-router.config.ts` sets `ssr: true`. `app/root.tsx` exports `links`, a `Layout` with `Meta`, `Links`, `ScrollRestoration` and `Scripts`, the `App` outlet, and an `ErrorBoundary` that reads `Route.ErrorBoundaryProps`, branches on `isRouteErrorResponse` for a 404 and shows the stack only when `import.meta.env.DEV`. `app/routes.ts` registers a single `index("routes/home.tsx")`, and `home.tsx` exports `meta` and renders `Welcome`.

The root boundary is a last resort: when it renders, the whole page is replaced, header and navigation included. The agent adds a shared `app/components/route-error.tsx` that takes `Route.ErrorBoundaryProps`, maps a thrown `data()` response to its status and statusText, an `Error` to its message, and anything else to a generic line, and shows a stack in development only, the same rule root.tsx already follows. It then exports an `ErrorBoundary` from `app/routes/home.tsx` that renders this component inside the existing page frame, so a failing loader on one route leaves the rest of the layout standing. In `app/routes.ts` it adds a `route("*", "routes/not-found.tsx")` whose loader throws `data(null, { status: 404 })` and whose boundary shows the not-found message, which gives the 404 a route of its own rather than a branch in the root boundary.

Errors that reach a boundary during server rendering are logged by the framework's default `handleError`, which prints to the console. The agent runs `npx react-router reveal` to write `app/entry.server.tsx`, then exports a `handleError` from it that ignores `request.signal.aborted`, logs route errors with the request method and URL, and leaves one marked function where a reporting client belongs. The root boundary keeps its text but loses its 404 branch, which the new route now owns, and `isRouteErrorResponse` stays for anything else that is thrown as a response.

The template has no unit tests; the templates repository runs Playwright from its `.tests` directory in `.github/workflows/ci.yml` against each template on pnpm 10 and Node 22. The agent adds `vitest`, `@testing-library/react` and `jsdom` as dev dependencies, a `vitest.config.ts` that reuses the existing Vite plugins, and `app/components/route-error.test.tsx` that renders the component with a thrown 404 `data()` response, an `Error` and an unknown value and asserts the text for each. It runs `npm run typecheck`, `npx vitest --run` and `npm run build`, starts `npm run start`, and `curl -i`s an unknown path to show the `404` status and the boundary's markup, pasting all of it in the pull request.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add error boundaries to this React Router 7 app and open a pull request. Read package.json, react-router.config.ts, vite.config.ts, tsconfig.json, app/root.tsx, app/routes.ts and app/routes/home.tsx first. Add app/components/route-error.tsx taking Route.ErrorBoundaryProps that renders status and statusText for a route error response, the message for an Error, a generic line otherwise, and the stack only in development, following the rule in root.tsx. Export an ErrorBoundary from app/routes/home.tsx that renders it inside the page frame. Add a splat route routes/not-found.tsx in app/routes.ts whose loader throws data(null, { status: 404 }) and whose ErrorBoundary shows a not-found message, and remove the 404 branch from the root boundary. Run npx react-router reveal to create app/entry.server.tsx and export a handleError that skips aborted requests, logs the method and URL with the error, and leaves one clearly marked function for a reporting client. Add vitest, @testing-library/react and jsdom as dev dependencies, a vitest.config.ts reusing the Vite plugins, and app/components/route-error.test.tsx covering the three error kinds. Run npm run typecheck, npx vitest --run and npm run build, start the server, curl -i an unknown path and paste the 404 response, the test summary and the typecheck output in the pull request. Do not change the Welcome component, the styles or the Dockerfile.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request with `app/components/route-error.tsx` and its test, `app/routes/not-found.tsx`, a new `app/entry.server.tsx` with `handleError`, an `ErrorBoundary` export in `app/routes/home.tsx`, one more line in `app/routes.ts`, a trimmed root boundary, and a `vitest.config.ts` with three dev dependencies. The description shows `curl -i` on an unknown path returning `HTTP/1.1 404` with the not-found markup, the vitest summary and the `react-router typegen && tsc` output with no errors.

The templates repository runs its Playwright suite on pull requests, which loads the template's home page, so a change that breaks the index route fails there. The boundaries themselves are exercised by the new vitest file and the pasted 404, which are the evidence for them until the team adds a Playwright case for an unknown path.

A reviewer checks that the stack is hidden outside development, that `handleError` skips aborted requests so navigations away do not log as failures, that the splat route is last in `routes.ts`, and that the root boundary still catches an error thrown from `Layout` itself. Then they wire the marked function in `entry.server.tsx` to their error tracker.

## Run it on every new route added

A team with several React Router apps sends one `POST /o/{org}/sessions` per repository with this prompt and that repository in `resources`. Each run reads its own `routes.ts`, so an app with nested layouts gets a boundary at each layout route rather than on the index alone, and each opens its own pull request on its own branch.

Send a `request_id` with each body so a retry returns the run already started. With a `webhook_url` on the API key, Rush POSTs `run.completed` when each turn ends, with the `session_url` to read the pull request from.

## Why run it on Rush?

Error handling is the code nobody sees until it runs, and in a Rush sandbox it runs: the agent builds the app with `react-router build`, starts the production server and requests a path that does not exist, and the status line and markup that came back are kept in the run's record with the command that produced them. A reviewer reads the real 404 rather than trusting that `data()` was thrown with the right status.

Revealing `entry.server.tsx` and writing `handleError` is a step that is easy to postpone on a laptop because the default works until the first production error vanishes into a console. The agent does it as part of the job, and the typed test for the boundary component means a later refactor that drops the development-only stack rule fails `vitest`. The same request body works unchanged on every React Router app a team runs.

## Frequently asked questions

### The template already has an ErrorBoundary in root.tsx. Why add more?

The root boundary replaces the whole document when it renders, navigation and all. A boundary exported from a route renders in that route's slot, so the layout around it survives a failing loader or a thrown response. The root one stays as the fallback for errors in `Layout` itself.

### What does handleError add?

React Router calls it on the server for every error that reaches a boundary during rendering or in a loader. The default logs to the console; the exported one skips requests the client already aborted, adds the method and URL, and gives the team one function to connect an error tracker to, without touching the components.

### Why a splat route for 404 instead of the root boundary branch?

A route of its own gets its own `meta`, its own markup inside the layout, and a loader that throws a real `data(null, { status: 404 })`, so the server answers with a 404 status rather than rendering an error page with a 200. The root boundary no longer needs to guess from `error.status`.

### Will the stack trace leak in production?

No. The component shows `error.stack` only when `import.meta.env.DEV` is true, the same condition `app/root.tsx` used before, and the test covers the branch. In the production build Vite replaces that flag with `false`.

## More React Router jobs

- [Add File Uploads to a React Router App](https://getrush.ai/run/add-file-uploads/react-router/)
- [Add Form Validation to a React Router App](https://getrush.ai/run/add-form-validation/react-router/)
- [Set Up GitHub Actions CI for a React Router App](https://getrush.ai/run/add-github-actions-ci/react-router/)

## The same job on other stacks

- [Add Error Boundaries to a SolidJS App](https://getrush.ai/run/add-error-boundaries/solidjs/)
- [Add Error Boundaries to a Vue App](https://getrush.ai/run/add-error-boundaries/vue/)
- [Add Error Boundaries to a React App](https://getrush.ai/run/add-error-boundaries/react/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
