From f64cd7e8f38f0c1fc1bbd85b5b5caf560565c024 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Fri, 21 Jul 2023 16:51:39 +0200 Subject: [PATCH] fix: Fix socket error crashing Zigbee2MQTT (#18388) --- lib/extension/frontend.ts | 1 + test/frontend.test.js | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/extension/frontend.ts b/lib/extension/frontend.ts index 31c45eda3c..77c710924f 100644 --- a/lib/extension/frontend.ts +++ b/lib/extension/frontend.ts @@ -119,6 +119,7 @@ export default class Frontend extends Extension { } @bind private onWebSocketConnection(ws: WebSocket): void { + ws.on('error', (msg) => logger.error(`WebSocket error: ${msg.message}`)); ws.on('message', (data: Buffer, isBinary: boolean) => { if (!isBinary && data) { const message = data.toString(); diff --git a/test/frontend.test.js b/test/frontend.test.js index 3c1f1287a8..fd6b3fa608 100644 --- a/test/frontend.test.js +++ b/test/frontend.test.js @@ -1,5 +1,5 @@ const data = require('./stub/data'); -require('./stub/logger'); +const logger = require('./stub/logger'); require('./stub/zigbeeHerdsman'); const MQTT = require('./stub/mqtt'); const settings = require('../lib/util/settings'); @@ -197,7 +197,7 @@ describe('Frontend', () => { }); - it('Websocket interaction', async () => { + it('onlythis Websocket interaction', async () => { controller = new Controller(jest.fn(), jest.fn()); await controller.start(); @@ -233,6 +233,10 @@ describe('Frontend', () => { mockWSClient.events.message(null, false); await flushPromises(); + // Error + mockWSClient.events.error(new Error('This is an error')); + expect(logger.error).toHaveBeenCalledWith('WebSocket error: This is an error'); + // Received message on socket expect(mockWSClient.implementation.send).toHaveBeenCalledTimes(1); expect(mockWSClient.implementation.send).toHaveBeenCalledWith(stringify({topic: 'bulb_color', payload: {state: 'ON', power_on_behavior:null, linkquality: null, update_available: null, update: {state: null, installed_version: -1, latest_version: -1}}}));