Add error handling: validate env vars, catch unhandled rejections, log fatal errors
This commit is contained in:
28
index.js
28
index.js
@@ -33,9 +33,21 @@ function loadQuotes() {
|
|||||||
|
|
||||||
console.log(`✅ Loaded ${quotes.length} Ancestor quotes`);
|
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 {
|
try {
|
||||||
await client.startClient();
|
await client.startClient();
|
||||||
console.log('✅ Client initialized\n');
|
console.log('✅ MatrixClient connected and authenticated\n');
|
||||||
|
|
||||||
let lastSentMessage = null;
|
let lastSentMessage = null;
|
||||||
|
|
||||||
@@ -100,12 +112,24 @@ function loadQuotes() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
console.log('🤖 Bot is running!');
|
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) {
|
} catch (err) {
|
||||||
console.error('Fatal error:', err.message);
|
console.error('Fatal error:', err.message);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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);
|
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