import { getLogger } from './logging.js'; import { isOperationalError } from './error-handling.js'; const logger = getLogger(); /** * Configure global unhandled error handlers */ export function setupGlobalErrorHandlers(): void { // Handle uncaught exceptions process.on('uncaughtException', (error) => { // Determine if error is operational or programming const isOperational = isOperationalError(error); // Log the error if (isOperational) { logger.warn('Uncaught operational exception:', error); } else { logger.error('Uncaught programming exception:', error); } // For non-operational errors, exit process after logging if (!isOperational) { logger.error('Process will exit due to unhandled programming error'); // Allow logs to be written before exiting setTimeout(() => { process.exit(1); }, 1000); } }); // Handle unhandled promise rejections process.on('unhandledRejection', (reason) => { // Convert the unhandled rejection to an unhandled exception logger.error('Unhandled Promise Rejection:', reason); throw reason; }); // Graceful shutdown on SIGTERM process.on('SIGTERM', () => { logger.info('SIGTERM received, shutting down gracefully'); process.exit(0); }); // Graceful shutdown on SIGINT process.on('SIGINT', () => { logger.info('SIGINT received, shutting down gracefully'); process.exit(0); }); logger.info('Global error handlers configured'); }