import React from 'react'; import errorLogger from '../utils/errorLogger'; class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false, error: null, errorInfo: null }; } static getDerivedStateFromError(error) { // 다음 렌더링에서 폴백 UI가 보이도록 상태를 업데이트합니다. return { hasError: true }; } componentDidCatch(error, errorInfo) { // 오류 정보를 상태에 저장 this.setState({ error: error, errorInfo: errorInfo }); // 오류 로깅 errorLogger.logError({ type: 'react_error_boundary', message: error.message, stack: error.stack, componentStack: errorInfo.componentStack, timestamp: new Date().toISOString(), url: window.location.href, props: this.props.errorContext || {} }); console.error('ErrorBoundary caught an error:', error, errorInfo); } handleReload = () => { window.location.reload(); }; handleGoHome = () => { window.location.href = '/'; }; handleReportError = () => { const errorReport = { error: this.state.error?.message, stack: this.state.error?.stack, componentStack: this.state.errorInfo?.componentStack, url: window.location.href, timestamp: new Date().toISOString(), userAgent: navigator.userAgent }; // 오류 보고서를 클립보드에 복사 navigator.clipboard.writeText(JSON.stringify(errorReport, null, 2)) .then(() => { alert('오류 정보가 클립보드에 복사되었습니다.'); }) .catch(() => { // 클립보드 복사 실패 시 텍스트 영역에 표시 const textarea = document.createElement('textarea'); textarea.value = JSON.stringify(errorReport, null, 2); document.body.appendChild(textarea); textarea.select(); document.execCommand('copy'); document.body.removeChild(textarea); alert('오류 정보가 클립보드에 복사되었습니다.'); }); }; render() { if (this.state.hasError) { return (
😵

앗! 문제가 발생했습니다

예상치 못한 오류가 발생했습니다.
이 문제는 자동으로 개발팀에 보고되었습니다.

{/* 개발 환경에서만 상세 오류 정보 표시 */} {process.env.NODE_ENV === 'development' && this.state.error && (
개발자 정보 (클릭하여 펼치기)
오류 메시지:
                                        {this.state.error.message}
                                    
스택 트레이스:
                                        {this.state.error.stack}
                                    
{this.state.errorInfo?.componentStack && (
컴포넌트 스택:
                                            {this.state.errorInfo.componentStack}
                                        
)}
)}
💡 도움말: 문제가 계속 발생하면 페이지를 새로고침하거나 브라우저 캐시를 삭제해보세요.
); } return this.props.children; } } export default ErrorBoundary;