Add error handling: validate env vars, catch unhandled rejections, log fatal errors
This commit is contained in:
30
index.js
30
index.js
@@ -32,10 +32,22 @@ function loadQuotes() {
|
||||
}
|
||||
|
||||
console.log(`✅ Loaded ${quotes.length} Ancestor quotes`);
|
||||
|
||||
// Validate environment variables first
|
||||
const token = process.env.MATRIX_ACCESS_TOKEN;
|
||||
const server = process.env.MATRIX_HOME_SERVER;
|
||||
|
||||
if (!token || !server) {
|
||||
console.error('❌ Missing required environment variables!');
|
||||
console.error(' Required: MATRIX_ACCESS_TOKEN, MATRIX_HOME_SERVER');
|
||||
console.error(' See .env.example for guidance.');
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`🏠 Server: ${server}`);
|
||||
|
||||
try {
|
||||
await client.startClient();
|
||||
console.log('✅ Client initialized\n');
|
||||
console.log('✅ MatrixClient connected and authenticated\n');
|
||||
|
||||
let lastSentMessage = null;
|
||||
|
||||
@@ -100,12 +112,24 @@ function loadQuotes() {
|
||||
});
|
||||
|
||||
console.log('🤖 Bot is running!');
|
||||
console.log(' Invite this bot to a Matrix room and type !quote to get an Ancestor quote.\n');
|
||||
console.log(' Invite this bot to a Matrix room and type to get an Ancestor quote.\n');
|
||||
|
||||
} catch (err) {
|
||||
console.error('Fatal error:', err.message);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
setInterval(() => {}, 60000);
|
||||
// Add global error handler to catch any unhandled errors
|
||||
process.on('unhandledRejection', (reason, promise) => {
|
||||
console.error('🚨 Unhandled rejection:', reason?.message || reason);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
// Keep the process alive
|
||||
setInterval(() => {}, 60000);
|
||||
} catch (err) {
|
||||
console.error('❌ Fatal error starting bot:', err.message);
|
||||
console.error(err.stack);
|
||||
process.exit(1);
|
||||
}
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user