feat(frontend): improve error handling and UX - Sprint 3 Story 4

Add comprehensive error handling with Error Boundary and improve user feedback.

Changes:
- Created global ErrorBoundary component with fallback UI using react-error-boundary
- Integrated ErrorBoundary in root layout to catch all errors
- Created Loading component with variants (sm, md, lg) for consistent loading states
- Created EmptyState component for better empty data display with CTAs
- Improved form error messages in login and register pages (consistent destructive styling)
- Updated projects page to use EmptyState component
- Added better error handling with retry actions

UX improvements:
- Better error messages and recovery options with clear action buttons
- Consistent loading indicators across all pages
- Helpful empty states with clear descriptions and CTAs
- Graceful error handling without crashes
- Consistent destructive color theme for all error messages

Technical:
- Installed react-error-boundary package (v5)
- All TypeScript types are properly defined
- Build and type checking pass successfully

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Yaojia Wang
2025-11-05 20:04:00 +01:00
parent 358ee9b7f4
commit 99ba4c4b1a
9 changed files with 204 additions and 43 deletions

View File

@@ -0,0 +1,59 @@
'use client';
import { ErrorBoundary as ReactErrorBoundary } from 'react-error-boundary';
import { AlertCircle } from 'lucide-react';
import { Button } from '@/components/ui/button';
interface ErrorFallbackProps {
error: Error;
resetErrorBoundary: () => void;
}
function ErrorFallback({ error, resetErrorBoundary }: ErrorFallbackProps) {
return (
<div className="flex min-h-[400px] flex-col items-center justify-center p-4">
<AlertCircle className="h-16 w-16 text-destructive mb-4" />
<h2 className="text-2xl font-bold mb-2">Something went wrong</h2>
<p className="text-muted-foreground mb-4 text-center max-w-md">
{error.message || 'An unexpected error occurred'}
</p>
<div className="flex gap-2">
<Button onClick={resetErrorBoundary}>
Try again
</Button>
<Button
variant="outline"
onClick={() => window.location.href = '/'}
>
Go to Dashboard
</Button>
</div>
</div>
);
}
interface ErrorBoundaryProps {
children: React.ReactNode;
}
export function ErrorBoundary({ children }: ErrorBoundaryProps) {
return (
<ReactErrorBoundary
FallbackComponent={ErrorFallback}
onReset={() => {
// Optional: Reset application state here
// For now, we'll just reload the current page
window.location.reload();
}}
onError={(error, errorInfo) => {
// Log error to console in development
console.error('Error caught by boundary:', error, errorInfo);
// In production, you could send this to an error tracking service
// like Sentry, LogRocket, etc.
}}
>
{children}
</ReactErrorBoundary>
);
}

View File

@@ -0,0 +1,44 @@
import { LucideIcon } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { cn } from '@/lib/utils';
interface EmptyStateProps {
icon: LucideIcon;
title: string;
description: string;
action?: {
label: string;
onClick: () => void;
variant?: 'default' | 'outline' | 'secondary';
};
className?: string;
}
export function EmptyState({
icon: Icon,
title,
description,
action,
className
}: EmptyStateProps) {
return (
<div className={cn(
'flex flex-col items-center justify-center py-12 px-4',
className
)}>
<Icon className="h-16 w-16 text-muted-foreground/50 mb-4" />
<h3 className="text-lg font-semibold mb-2">{title}</h3>
<p className="text-muted-foreground text-center mb-6 max-w-sm">
{description}
</p>
{action && (
<Button
onClick={action.onClick}
variant={action.variant || 'default'}
>
{action.label}
</Button>
)}
</div>
);
}

37
components/ui/loading.tsx Normal file
View File

@@ -0,0 +1,37 @@
import { Loader2 } from 'lucide-react';
import { cn } from '@/lib/utils';
interface LoadingProps {
className?: string;
text?: string;
size?: 'sm' | 'md' | 'lg';
}
export function Loading({ className, text, size = 'md' }: LoadingProps) {
const sizeClasses = {
sm: 'h-4 w-4',
md: 'h-6 w-6',
lg: 'h-8 w-8',
};
return (
<div className={cn('flex items-center justify-center', className)}>
<Loader2 className={cn(sizeClasses[size], 'animate-spin mr-2')} />
{text && <span className="text-muted-foreground">{text}</span>}
</div>
);
}
// Full page loading component
export function LoadingPage({ text = 'Loading...' }: { text?: string }) {
return (
<div className="flex min-h-[400px] items-center justify-center">
<Loading text={text} size="lg" />
</div>
);
}
// Inline loading for buttons or small areas
export function LoadingInline({ text }: { text?: string }) {
return <Loading text={text} size="sm" />;
}