Refactor error handling and startup logic in index.js

This commit is contained in:
Christopher Cookman 2024-05-08 08:33:32 -06:00
parent 2db900e154
commit f0b45c190b
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42

View file

@ -85,17 +85,20 @@ const xmpp = client({
//debug(xmpp, true); //debug(xmpp, true);
xmpp.on("error", (err) => { xmpp.on("error", (err) => {
console.log("ERROR") console.log(`ERR: ${err}`);
console.error(err);
setTimeout(() => { setTimeout(() => {
xmpp.stop().then(() => {
start(); start();
});
}, 5000); }, 5000);
}); });
xmpp.on("offline", () => { xmpp.on("offline", () => {
console.log("offline"); console.log("offline");
setTimeout(() => { setTimeout(() => {
xmpp.stop().then(() => {
start(); start();
});
}, 5000); }, 5000);
}); });
@ -244,14 +247,12 @@ xmpp.on("online", async (address) => {
}); });
const start = () => { const start = () => {
xmpp.stop().then(() => {
xmpp.start().catch((err) => { xmpp.start().catch((err) => {
console.error(`start failed, ${err}\nGonna try again in 5 seconds...`); console.error(`start failed, ${err}\nGonna try again in 5 seconds...`);
setTimeout(() => { setTimeout(() => {
start(); start();
}, 5000); }, 5000);
}); });
}); // Do this just in case
} }
// END XMPP // END XMPP
@ -511,5 +512,14 @@ discord.on("interactionCreate", async (interaction) => {
} }
}); });
process.on("unhandledRejection", (error) => {
console.error("Unhandled promise rejection:", error);
});
process.on("uncaughtException", (error) => {
console.error("Uncaught exception:", error);
});
// Login to discord // Login to discord
discord.login(config.discord.token); discord.login(config.discord.token);