From 43c53878abb97b9ade10a593f01b20624b503b3d Mon Sep 17 00:00:00 2001 From: Ryan Florence Date: Wed, 20 Nov 2024 15:38:07 -0700 Subject: [PATCH] docs: added error reporting --- docs/how-to/error-boundary.md | 1 - docs/how-to/error-reporting.md | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/docs/how-to/error-boundary.md b/docs/how-to/error-boundary.md index 296b152237..3bd4daa6e8 100644 --- a/docs/how-to/error-boundary.md +++ b/docs/how-to/error-boundary.md @@ -22,7 +22,6 @@ import { Route } from "./+types/root"; export function ErrorBoundary({ error, }: Route.ErrorBoundaryProps) { - console.error(error); if (isRouteErrorResponse(error)) { return ( <> diff --git a/docs/how-to/error-reporting.md b/docs/how-to/error-reporting.md index 9dad8283b7..685d0ea0f6 100644 --- a/docs/how-to/error-reporting.md +++ b/docs/how-to/error-reporting.md @@ -1,4 +1,36 @@ --- title: Error Reporting -hidden: true --- + +# Error Reporting + +React Router catches errors in your route modules and sends them to [error boundaries](./error-boundary) to prevent blank pages when errors occur. However, ErrorBoundary isn't sufficient for logging and reporting errors. To access these caught errors, use the handleError export of the server entry module. + +## 1. Reveal the server entry + +If you don't see `entry.server.tsx` in your app directory, you're using a default entry. Reveal it with this cli command: + +```shellscript nonumber +react-router reveal +``` + +## 2. Export your error handler + +This function is called whenever React Router catches an error in your application on the server. + +```tsx filename=entry.server.tsx +import { type HandleErrorFunction } from "react-router"; + +export const handleError: HandleErrorFunction = ( + error, + { request } +) => { + // React Router may abort some interrupted requests, don't log those + if (!request.signal.aborted) { + myReportError(error); + + // make sure to still log the error so you can see it + console.error(error); + } +}; +```