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(() => {
start(); xmpp.stop().then(() => {
start();
});
}, 5000); }, 5000);
}); });
xmpp.on("offline", () => { xmpp.on("offline", () => {
console.log("offline"); console.log("offline");
setTimeout(() => { setTimeout(() => {
start(); xmpp.stop().then(() => {
start();
});
}, 5000); }, 5000);
}); });
@ -127,14 +130,14 @@ xmpp.on("stanza", (stanza) => {
messages++; messages++;
// Handle NTFY // Handle NTFY
if (config.ntfy.enabled) { if (config.ntfy.enabled) {
if(config.debug >= 1) console.log(`Sending NTFY for ${config.ntfy.prefix}${fromChannel}`) if (config.debug >= 1) console.log(`Sending NTFY for ${config.ntfy.prefix}${fromChannel}`)
ntfyBody = { ntfyBody = {
"topic": `${config.ntfy.prefix}${fromChannel}`, "topic": `${config.ntfy.prefix}${fromChannel}`,
"message": bodyData.string, "message": bodyData.string,
"title": "New Alert", "title": "New Alert",
"tags": [`Timestamp: ${product_id.timestamp}`, `Station: ${product_id.station}`, `WMO: ${product_id.wmo}`, `PIL: ${product_id.pil}`, `Channel: ${fromChannel}`], "tags": [`Timestamp: ${product_id.timestamp}`, `Station: ${product_id.station}`, `WMO: ${product_id.wmo}`, `PIL: ${product_id.pil}`, `Channel: ${fromChannel}`],
"priority": 3, "priority": 3,
"actions": [{ "action": "view", "label": "Product", "url": bodyData.url }, { "action": "view", "label": "Product Text", "url": `https://mesonet.agron.iastate.edu/api/1/nwstext/${product_id_raw}`}] "actions": [{ "action": "view", "label": "Product", "url": bodyData.url }, { "action": "view", "label": "Product Text", "url": `https://mesonet.agron.iastate.edu/api/1/nwstext/${product_id_raw}` }]
} }
if (stanza.getChild("x").attrs.twitter_media) { if (stanza.getChild("x").attrs.twitter_media) {
ntfyBody.attach = stanza.getChild("x").attrs.twitter_media; ntfyBody.attach = stanza.getChild("x").attrs.twitter_media;
@ -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
@ -438,7 +439,7 @@ discord.on("interactionCreate", async (interaction) => {
fields: [ fields: [
{ {
name: "Uptime", name: "Uptime",
value: `Since <t:${Math.floor(startTimestap/1000)}>, Started <t:${Math.floor(startTimestap/1000)}:R>`, value: `Since <t:${Math.floor(startTimestap / 1000)}>, Started <t:${Math.floor(startTimestap / 1000)}:R>`,
}, },
{ {
name: "Caught Messages", name: "Caught Messages",
@ -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);