Spaces:
Sleeping
Sleeping
File size: 681 Bytes
1cf0854 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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 })
});
}; |