feat: add enhanced error handling and logging in main process

main
borja (aider) 3 months ago
parent d16823bbaf
commit 575b140820

@ -8,6 +8,19 @@ dotenv.config();
// Get port from environment or default to 3000 // Get port from environment or default to 3000
const PORT = process.env.PORT ? parseInt(process.env.PORT) : 3000; const PORT = process.env.PORT ? parseInt(process.env.PORT) : 3000;
// Enhanced error logging
function logError(error: unknown) {
if (error instanceof Error) {
console.error('Error:', error.message);
console.error('Stack:', error.stack);
if (error.cause) {
console.error('Caused by:', error.cause);
}
} else {
console.error('Unknown error:', error);
}
}
// Start the bot and server // Start the bot and server
async function main() { async function main() {
try { try {
@ -18,21 +31,49 @@ async function main() {
console.log('Starting server...'); console.log('Starting server...');
startServer(PORT); startServer(PORT);
console.log(`Server running on port ${PORT}`); console.log(`Server running on port ${PORT}`);
// Keep process alive
setInterval(() => {
// Just keep the event loop running
}, 1000);
} catch (error) { } catch (error) {
console.error('Error starting application:', error); console.error('\n=== APPLICATION FAILED TO START ===');
logError(error);
console.error('===============================');
process.exit(1); process.exit(1);
} }
} }
// Handle process termination // Handle process termination
process.on('SIGINT', () => { process.on('SIGINT', () => {
console.log('Shutting down gracefully...'); console.log('\nShutting down gracefully...');
process.exit(0); process.exit(0);
}); });
process.on('SIGTERM', () => { process.on('SIGTERM', () => {
console.log('Shutting down gracefully...'); console.log('\nShutting down gracefully...');
process.exit(0); process.exit(0);
}); });
main(); process.on('uncaughtException', (error) => {
console.error('\n=== UNCAUGHT EXCEPTION ===');
logError(error);
console.error('=========================');
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('\n=== UNHANDLED REJECTION ===');
console.error('Promise:', promise);
console.error('Reason:', reason);
console.error('=========================');
process.exit(1);
});
main().catch((error) => {
console.error('\n=== MAIN FUNCTION FAILED ===');
logError(error);
console.error('=========================');
process.exit(1);
});

Loading…
Cancel
Save