test / src /middleware /error.middleware.ts
bipulsardar's picture
23
1d7d958
Raw History Blame Contribute Delete
536 Bytes
import { Request, Response, NextFunction } from 'express';
import { logger } from '../utils/logger';
export function errorHandler(
err: any,
req: Request,
res: Response,
next: NextFunction
): void {
logger.error(`Unhandled HTTP Error [${req.method} ${req.path}]:`, err);
const statusCode = err.status || err.statusCode || 500;
const message = err.message || 'Internal Server Error';
res.status(statusCode).json({
error: {
message,
statusCode,
timestamp: new Date().toISOString(),
},
});
}