Implement comprehensive error handling
- Add try-catch blocks to all server actions with retry logic - Implement exponential backoff retry mechanism for database operations - Add input validation to all server actions - Create ErrorBoundary component for React error handling - Add page-level error handlers (error.tsx, global-error.tsx) - Add route-specific error pages for list and item routes - Enhance database connection with graceful error handling - Add user-friendly error messages throughout the application - Implement proper error logging for debugging Task: 16. Implement error handling Requirements: All (error handling is cross-cutting)
This commit is contained in:
18
lib/db.ts
18
lib/db.ts
@@ -4,6 +4,22 @@ const globalForPrisma = globalThis as unknown as {
|
||||
prisma: PrismaClient | undefined
|
||||
}
|
||||
|
||||
export const prisma = globalForPrisma.prisma ?? new PrismaClient()
|
||||
// Create Prisma client with connection error handling
|
||||
export const prisma = globalForPrisma.prisma ?? new PrismaClient({
|
||||
log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
|
||||
})
|
||||
|
||||
// Handle connection errors
|
||||
prisma.$connect().catch((error) => {
|
||||
console.error('Failed to connect to database:', error)
|
||||
// Don't throw here - let individual queries handle connection errors
|
||||
})
|
||||
|
||||
// Graceful shutdown
|
||||
if (typeof window === 'undefined') {
|
||||
process.on('beforeExit', async () => {
|
||||
await prisma.$disconnect()
|
||||
})
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
|
||||
Reference in New Issue
Block a user