Spaces:
Sleeping
Sleeping
import { Request, Response, NextFunction } from 'express'; | |
export interface AppError extends Error { | |
statusCode?: number; | |
isOperational?: boolean; | |
} | |
export const errorHandler = ( | |
error: AppError, | |
req: Request, | |
res: Response, | |
next: NextFunction | |
) => { | |
const statusCode = error.statusCode || 500; | |
const message = error.message || 'Internal Server Error'; | |
// Log error for debugging | |
console.error('Error:', error); | |
// Don't leak error details in production | |
const isDevelopment = process.env.NODE_ENV === 'development'; | |
res.status(statusCode).json({ | |
error: message, | |
...(isDevelopment && { stack: error.stack }) | |
}); | |
}; |