2025-09-17 18:43:43 +09:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { Component, ReactNode, ErrorInfo } from "react";
|
2025-11-05 15:47:06 +09:00
|
|
|
import { log } from "@/lib/logger";
|
2025-12-16 13:54:31 +09:00
|
|
|
import { Button } from "@/components/atoms/button";
|
2025-09-17 18:43:43 +09:00
|
|
|
|
|
|
|
|
interface ErrorBoundaryState {
|
|
|
|
|
hasError: boolean;
|
|
|
|
|
error?: Error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ErrorBoundaryProps {
|
|
|
|
|
children: ReactNode;
|
|
|
|
|
fallback?: ReactNode;
|
|
|
|
|
onError?: (error: Error, errorInfo: ErrorInfo) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Error boundary component for catching and handling React errors
|
|
|
|
|
*/
|
|
|
|
|
export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
|
|
|
constructor(props: ErrorBoundaryProps) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = { hasError: false };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
|
|
|
|
|
return { hasError: true, error };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
2025-09-19 16:34:10 +09:00
|
|
|
// Log to external error service in production
|
2025-09-25 17:42:36 +09:00
|
|
|
if (process.env.NODE_ENV === "production") {
|
2025-10-28 13:43:45 +09:00
|
|
|
// Integration point: Send to error tracking service (Sentry, LogRocket, etc.)
|
|
|
|
|
// Example: Sentry.captureException(error, { contexts: { react: { componentStack: info.componentStack } } });
|
2025-09-19 16:34:10 +09:00
|
|
|
} else {
|
2025-09-25 17:42:36 +09:00
|
|
|
log.error("ErrorBoundary caught an error", {
|
|
|
|
|
error: error.message,
|
|
|
|
|
stack: error.stack,
|
|
|
|
|
componentStack: errorInfo.componentStack,
|
2025-09-20 11:35:40 +09:00
|
|
|
});
|
2025-09-19 16:34:10 +09:00
|
|
|
}
|
2025-09-17 18:43:43 +09:00
|
|
|
this.props.onError?.(error, errorInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override render() {
|
|
|
|
|
if (this.state.hasError) {
|
|
|
|
|
if (this.props.fallback) {
|
|
|
|
|
return this.props.fallback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex items-center justify-center h-64">
|
|
|
|
|
<div className="text-center space-y-4">
|
2025-12-16 18:12:12 +09:00
|
|
|
<h2 className="text-lg font-semibold text-danger">Something went wrong</h2>
|
2025-12-16 13:54:31 +09:00
|
|
|
<p className="text-muted-foreground">
|
|
|
|
|
{process.env.NODE_ENV === "development"
|
|
|
|
|
? this.state.error?.message || "An unexpected error occurred"
|
|
|
|
|
: "An unexpected error occurred. Please try again."}
|
2025-09-17 18:43:43 +09:00
|
|
|
</p>
|
2025-12-16 13:54:31 +09:00
|
|
|
<Button onClick={() => this.setState({ hasError: false, error: undefined })}>
|
2025-09-17 18:43:43 +09:00
|
|
|
Try again
|
2025-12-16 13:54:31 +09:00
|
|
|
</Button>
|
2025-09-17 18:43:43 +09:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.props.children;
|
|
|
|
|
}
|
|
|
|
|
}
|