diff --git a/app/(dashboard)/task/error.tsx b/app/(dashboard)/task/error.tsx new file mode 100644 index 0000000..f2edbba --- /dev/null +++ b/app/(dashboard)/task/error.tsx @@ -0,0 +1,364 @@ +'use client'; + +import { useEffect, useState, useTransition } from 'react'; +import Image from 'next/image'; +import Link from 'next/link'; +import { useRouter } from 'next/navigation'; +import { motion, AnimatePresence } from 'motion/react'; +import { + RotateCw, + ArrowLeft, + Home, + AlertTriangle, + Terminal, + Copy, + Check, + ChevronDown, + ChevronUp, + ShieldCheck, + Server, + WifiOff, + LifeBuoy, + Info, +} from 'lucide-react'; +import { Button } from '@/components/ui/button'; +import { Badge } from '@/components/ui/badge'; +import { Card900, CardContent } from '@/components/ui/card'; +import { toast } from 'sonner'; +import { clientLogger } from '@/lib/logger-client'; + +interface TaskErrorProps { + error: Error & { digest?: string }; + reset: () => void; +} + +export default function TaskError({ error, reset }: TaskErrorProps) { + const router = useRouter(); + const [isPending, startTransition] = useTransition(); + const [showDetails, setShowDetails] = useState(false); + const [copied, setCopied] = useState(false); + const [retrying, setRetrying] = useState(false); + + useEffect(() => { + try { + console.error('[TaskPageError]', { + name: error?.name, + message: error?.message, + digest: error?.digest, + stack: error?.stack, + }); + if (error?.stack) { + clientLogger.error( + `[TaskPageError] ${error.message} (digest: ${error.digest || 'none'})\n${error.stack}` + ); + } + } catch { + // Ignore logging fallback errors + } + }, [error]); + + const rawMessage = error?.message || ''; + const digest = error?.digest; + + // Detect if Next.js sanitized the error message in production + const isProductionOmitted = + rawMessage.includes('Server Components render') || + rawMessage.includes('specific message is omitted in production') || + rawMessage.includes('digest property is included'); + + const is500Error = + rawMessage.includes('500') || + rawMessage.toLowerCase().includes('internal server error'); + + const isNetworkError = + rawMessage.toLowerCase().includes('fetch failed') || + rawMessage.toLowerCase().includes('econnrefused') || + rawMessage.toLowerCase().includes('network') || + rawMessage.toLowerCase().includes('timeout'); + + // Context-sensitive status title and human-friendly explanation + let badgeText = 'Backend Service Disconnected'; + let headline = 'Unable to Load Task & Client Data'; + let friendlyDescription = + 'The system was unable to retrieve client records and task assignments from the backend service. This usually occurs when the backend server is temporarily unreachable, restarting, or under maintenance.'; + + if (is500Error) { + badgeText = 'Backend Server Error (500)'; + headline = 'Backend Service Encountered an Error'; + friendlyDescription = + 'The API server responded with an internal server error while fetching task records. Backend services may currently be undergoing a reload or processing a heavy task.'; + } else if (isNetworkError) { + badgeText = 'Network Unreachable'; + headline = 'Connection to API Server Lost'; + friendlyDescription = + 'A secure connection to the data server could not be established. Please check your internet connectivity or re-verify that your login session is still active.'; + } else if (!isProductionOmitted && rawMessage.trim().length > 0) { + badgeText = 'Task Service Error'; + headline = 'Failed to Retrieve Task Records'; + friendlyDescription = rawMessage; + } + + const handleRetry = () => { + setRetrying(true); + startTransition(() => { + try { + reset(); + router.refresh(); + } catch { + window.location.reload(); + } + }); + + // Fallback hard refresh if soft reset doesn't resolve within 2.5 seconds + setTimeout(() => { + window.location.reload(); + }, 2500); + }; + + const handleCopyDiagnostics = async () => { + const diagnosticsData = { + component: 'TaskDashboardError', + timestamp: new Date().toISOString(), + digest: digest || 'N/A', + errorName: error?.name || 'Error', + errorMessage: rawMessage, + detectedType: is500Error ? 'HTTP 500' : isNetworkError ? 'Network Timeout' : isProductionOmitted ? 'Production Sanitized' : 'Application Error', + stack: error?.stack || 'No stack trace available', + url: typeof window !== 'undefined' ? window.location.href : '', + userAgent: typeof navigator !== 'undefined' ? navigator.userAgent : '', + }; + + try { + await navigator.clipboard.writeText(JSON.stringify(diagnosticsData, null, 2)); + setCopied(true); + toast.success('Diagnostic report copied to clipboard'); + setTimeout(() => setCopied(false), 2500); + } catch { + toast.error('Failed to copy diagnostics'); + } + }; + + return ( +
+ {friendlyDescription} +
+ + {/* Actions Group */} +
+
+ Use this information when reporting bugs or inspecting backend server logs. +
+{error?.name || 'Error'}
+{digest || 'N/A'}
++ {is500Error ? 'HTTP 500' : isNetworkError ? 'Network Timeout' : isProductionOmitted ? 'Server Component' : 'API Failure'} +
+{new Date().toLocaleTimeString()}
+
+ {error.stack}
+
+ + If this message persists after several retries, copy the diagnostic report below and share it with the dev team. +
+