SocketIORouter: Don't crash if message handler throws

This commit is contained in:
Richard Hansen 2021-09-06 05:51:57 -04:00
parent 320e5c1109
commit 9f9adb369b
2 changed files with 25 additions and 2 deletions

View File

@ -78,7 +78,11 @@ exports.setSocketIO = (_io) => {
return;
}
logger.debug(`from ${socket.id}: ${JSON.stringify(message)}`);
await components[message.component].handleMessage(socket, message);
try {
await components[message.component].handleMessage(socket, message);
} catch (err) {
logger.error(`Error while handling message from ${socket.id}: ${err.stack || err}`);
}
});
socket.on('disconnect', (reason) => {

View File

@ -460,7 +460,7 @@ describe(__filename, function () {
await disconnected;
});
it('handleMessage', async function () {
it('handleMessage (success)', async function () {
let serverSocket;
const want = {
component: this.test.fullTitle(),
@ -480,5 +480,24 @@ describe(__filename, function () {
socket.send(want);
assert.deepEqual(await got, want);
});
it('handleMessage (error)', async function () {
let receive;
const received = new Promise((resolve) => receive = resolve);
socketIoRouter.addComponent(this.test.fullTitle(), new class extends Module {
handleMessage(socket, message) {
if (message.throw) throw new Error('injected');
receive();
}
}());
socket = await connect();
const tx = (msg = {}) => {
msg = Object.assign({component: this.test.fullTitle(), protocolVersion: 2}, msg);
socket.send(msg);
};
tx({throw: true});
tx();
await received;
});
});
});