Skip to content

Commit 584173c

Browse files
committed
feat: add top-level ErrorBoundary component
Add decoupled ErrorBoundary component that determines which error reporting service to use. Currently supports Bugsnag, but designed to be extensible for other services. Integrate into app root.
1 parent d58f11b commit 584173c

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import Bugsnag from "@bugsnag/js";
2+
import React, { type ReactNode } from "react";
3+
4+
import { ErrorHandler } from "@/components/shared/ErrorHandler";
5+
import { getBugsnagConfig } from "@/services/errorManagement/bugsnag";
6+
7+
interface ErrorBoundaryProps {
8+
children: ReactNode;
9+
}
10+
11+
/**
12+
* Generic ErrorBoundary that determines which error reporting service to use.
13+
* Currently supports Bugsnag, but can be extended to support other services.
14+
*/
15+
export const ErrorBoundary = ({ children }: ErrorBoundaryProps) => {
16+
const bugsnagConfig = getBugsnagConfig();
17+
18+
// Use Bugsnag's ErrorBoundary if enabled
19+
if (bugsnagConfig.enabled) {
20+
const BugsnagBoundary = Bugsnag.getPlugin("react")!.createErrorBoundary(
21+
React,
22+
);
23+
24+
return (
25+
<BugsnagBoundary
26+
FallbackComponent={(props: {
27+
error: Error;
28+
info: React.ErrorInfo;
29+
clearError: () => void;
30+
}) => (
31+
<ErrorHandler
32+
error={props.error}
33+
errorType="app_error_boundary"
34+
resetErrorBoundary={props.clearError}
35+
/>
36+
)}
37+
>
38+
{children}
39+
</BugsnagBoundary>
40+
);
41+
}
42+
43+
// Fallback: no error boundary (just render children)
44+
// In the future, this could check for other error reporting services
45+
return <>{children}</>;
46+
};

src/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { StrictMode } from "react";
77
import ReactDOM from "react-dom/client";
88
import { scan } from "react-scan";
99

10+
import { ErrorBoundary } from "@/components/shared/ErrorBoundary";
11+
1012
import { router } from "./routes/router";
1113
import { initializeBugsnag } from "./services/errorManagement/bugsnag";
1214

@@ -26,9 +28,11 @@ if (!rootElement.innerHTML) {
2628
const root = ReactDOM.createRoot(rootElement);
2729
root.render(
2830
<StrictMode>
29-
<QueryClientProvider client={queryClient}>
30-
<RouterProvider router={router} />
31-
</QueryClientProvider>
31+
<ErrorBoundary>
32+
<QueryClientProvider client={queryClient}>
33+
<RouterProvider router={router} />
34+
</QueryClientProvider>
35+
</ErrorBoundary>
3236
</StrictMode>,
3337
);
3438
}

0 commit comments

Comments
 (0)