errorHandler.ts•1.14 kB
import { Request, Response, NextFunction } from 'express';
import logger from '../utils/logger';
export class AppError extends Error {
statusCode: number;
status: string;
isOperational: boolean;
constructor(message: string, statusCode: number) {
super(message);
this.statusCode = statusCode;
this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error';
this.isOperational = true;
Error.captureStackTrace(this, this.constructor);
}
}
export const errorHandler = (
err: Error | AppError,
req: Request,
res: Response,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
next: NextFunction
) => {
if (err instanceof AppError) {
logger.error({
message: err.message,
statusCode: err.statusCode,
stack: err.stack,
});
return res.status(err.statusCode).json({
status: err.status,
message: err.message,
});
}
// Programming or other unknown error
logger.error({
message: err.message,
error: err,
stack: err.stack,
});
return res.status(500).json({
status: 'error',
message: 'Internal server error',
});
};