Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/components/shared/ErrorHandler.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Bugsnag from "@bugsnag/js";
import { useEffect } from "react";

import { ErrorDisplay } from "@/components/shared/ErrorDisplay";
import { getBugsnagConfig } from "@/services/errorManagement/bugsnag";

interface ErrorHandlerProps {
error: unknown;
errorType: "app_error_boundary" | "router_error";
resetErrorBoundary?: () => void;
onGoHome?: () => void;
}

/**
* Unified error handler component used for both React Error Boundaries and Router errors.
* Automatically reports errors to Bugsnag and provides a user-friendly error display.
*/
export const ErrorHandler = ({
error,
errorType,
resetErrorBoundary,
onGoHome,
}: ErrorHandlerProps) => {
useEffect(() => {
const config = getBugsnagConfig();

if (config.enabled && error instanceof Error) {
Bugsnag.notify(error, (event) => {
event.addMetadata("error_handler", {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we remove this magic string?

type: errorType,
pathname: window.location.pathname,
});
});
}
}, [error, errorType]);

const handleRefresh = () => {
// Reset error boundary if available before reloading
resetErrorBoundary?.();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this used for? seems like its only used in this file. lets remove it if we don't need it.

window.location.reload();
};

const handleGoHomeClick = () => {
// Reset error boundary if available
resetErrorBoundary?.();

// Use custom handler if provided, otherwise use window.location
if (onGoHome) {
onGoHome();
} else {
window.location.href = "/";
}
Comment on lines +48 to +52
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can just default onGoHome to be

onGoHome = () => window.location.href = "/";

NIT: Do we rename this to something else? onGoToSafeURL or something

};

return (
<ErrorDisplay
error={error}
onRefresh={handleRefresh}
onGoHome={handleGoHomeClick}
/>
);
};
10 changes: 3 additions & 7 deletions src/routes/FullPageError.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type ErrorComponentProps, useRouter } from "@tanstack/react-router";

import { ErrorDisplay } from "@/components/shared/ErrorDisplay";
import { ErrorHandler } from "@/components/shared/ErrorHandler";

export default function FullPageError({ error }: ErrorComponentProps) {
const router = useRouter();
Expand All @@ -9,14 +9,10 @@ export default function FullPageError({ error }: ErrorComponentProps) {
router.navigate({ to: "/" });
};

const handleRefresh = () => {
window.location.reload();
};

return (
<ErrorDisplay
<ErrorHandler
error={error}
onRefresh={handleRefresh}
errorType="router_error"
onGoHome={handleGoHome}
/>
);
Expand Down