SaaS_Backend / src /middleware /errorHandler.ts
ChiragPatankar's picture
Upload 33 files
1cf0854 verified
raw
history blame contribute delete
681 Bytes
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 })
});
};