From bca8efef237cbd3370fb61596825d89f0e8893e6 Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Thu, 8 May 2025 01:17:31 -0600 Subject: [PATCH] Fix crash when non-json data --- index.js | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 4194940..d585a09 100644 --- a/index.js +++ b/index.js @@ -90,7 +90,19 @@ app.ws('/iem', (ws, req) => { }); try { ws.on('message', (msg) => { - const data = JSON.parse(msg); + let data; + try { + data = JSON.parse(msg); + } catch (error) { + ws.send(JSON.stringify({ + "type": "internal-response", + "code": 400, + "data": { + "error": "Invalid JSON format." + } + })); + return; + } if (!data.type) return ws.send(JSON.stringify({ "type": "internal-response", "code": 400, @@ -191,6 +203,15 @@ app.ws('/iem', (ws, req) => { })); } break; + case "ping": + ws.send(JSON.stringify({ + "type": "internal-response", + "code": 200, + "data": { + "message": "pong" + } + })); + break; default: ws.send(JSON.stringify({ "type": "internal-response", @@ -551,25 +572,6 @@ xmpp.reconnect.on("reconnected", () => { }); }) -const createDiscordEmbed = (data) => { - const embed = { - description: ` ${data.body}`, - color: parseInt(config.priorityColors[data.event.priority].replace("#", ""), 16) || 0x000000, - timestamp: new Date(data.timestamp).toISOString(), - footer: { - text: `Station: ${data.station} PID: ${data.raw} Channel: ${data.channel}` - } - }; - - if (data.image) { - embed.image = { - url: data.image - }; - } - - return embed; -}; - xmpp.on("online", async (address) => {