"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleError = exports.AppError = void 0;
const logger_1 = __importDefault(require("./logger"));
class AppError extends Error {
constructor(message, statusCode, isOperational = true) {
super(message);
this.statusCode = statusCode;
this.isOperational = isOperational;
Error.captureStackTrace(this, this.constructor);
}
}
exports.AppError = AppError;
const handleError = (err) => {
if (err instanceof AppError && err.isOperational) {
logger_1.default.error({
message: err.message,
statusCode: err.statusCode,
stack: err.stack
});
}
else {
// Unhandled error - application should crash
logger_1.default.error({
message: 'Unhandled error',
error: err.message,
stack: err.stack
});
process.exit(1);
}
};
exports.handleError = handleError;
// Global uncaught exception handler
process.on('uncaughtException', (err) => {
logger_1.default.error('UNCAUGHT EXCEPTION! Shutting down...');
logger_1.default.error(err.name, err.message, err.stack);
process.exit(1);
});
// Global unhandled rejection handler
process.on('unhandledRejection', (reason) => {
logger_1.default.error('UNHANDLED REJECTION! Shutting down...');
logger_1.default.error(reason);
process.exit(1);
});