SocketIORouter: Improve error handling

pull/5433/head
Richard Hansen 2022-02-23 15:41:23 -05:00
parent fcfa51bda6
commit 3b76b2dd67
1 changed files with 11 additions and 11 deletions

View File

@ -68,19 +68,19 @@ exports.setSocketIO = (_io) => {
components[i].handleConnect(socket);
}
socket.on('message', (message, ack = () => {}) => {
socket.on('message', (message, ack = () => {}) => (async () => {
if (!message.component || !components[message.component]) {
logger.error(`Can't route the message: ${JSON.stringify(message)}`);
return;
throw new Error(`unknown message component: ${message.component}`);
}
logger.debug(`from ${socket.id}: ${JSON.stringify(message)}`);
(async () => await components[message.component].handleMessage(socket, message))().then(
(val) => ack(null, val),
(err) => {
logger.error(`Error while handling message from ${socket.id}: ${err.stack || err}`);
ack({name: err.name, message: err.message});
});
});
logger.debug(`from ${socket.id}:`, message);
return await components[message.component].handleMessage(socket, message);
})().then(
(val) => ack(null, val),
(err) => {
logger.error(
`Error handling ${message.component} message from ${socket.id}: ${err.stack || err}`);
ack({name: err.name, message: err.message}); // socket.io can't handle Error objects.
}));
socket.on('disconnect', (reason) => {
logger.debug(`${socket.id} disconnected: ${reason}`);