import { Request, Response, NextFunction } from 'express'; import { ZodError } from 'zod'; export function errorHandler( err: unknown, req: Request, res: Response, next: NextFunction ): void { if (err instanceof ZodError) { res.status(400).json({ error: 'Validation error', details: err.flatten() }); return; } if (err instanceof Error) { const status = (err as NodeJS.ErrnoException & { status?: number }).status ?? 500; const message = status < 500 ? err.message : 'Internal server error'; if (status >= 500) console.error(err); res.status(status).json({ error: message }); return; } res.status(500).json({ error: 'Internal server error' }); }