Fix crash when non-json data
This commit is contained in:
parent
26f6e7e9c2
commit
bca8efef23
42
index.js
42
index.js
|
@ -90,7 +90,19 @@ app.ws('/iem', (ws, req) => {
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
ws.on('message', (msg) => {
|
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({
|
if (!data.type) return ws.send(JSON.stringify({
|
||||||
"type": "internal-response",
|
"type": "internal-response",
|
||||||
"code": 400,
|
"code": 400,
|
||||||
|
@ -191,6 +203,15 @@ app.ws('/iem', (ws, req) => {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case "ping":
|
||||||
|
ws.send(JSON.stringify({
|
||||||
|
"type": "internal-response",
|
||||||
|
"code": 200,
|
||||||
|
"data": {
|
||||||
|
"message": "pong"
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
ws.send(JSON.stringify({
|
ws.send(JSON.stringify({
|
||||||
"type": "internal-response",
|
"type": "internal-response",
|
||||||
|
@ -551,25 +572,6 @@ xmpp.reconnect.on("reconnected", () => {
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
const createDiscordEmbed = (data) => {
|
|
||||||
const embed = {
|
|
||||||
description: `<t:${Math.floor(data.timestamp / 1000)}:T> <t:${Math.floor(data.timestamp / 1000)}:R> ${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) => {
|
xmpp.on("online", async (address) => {
|
||||||
|
|
Loading…
Reference in a new issue