Skip to content

Commit

Permalink
fix: Fix socket error crashing Zigbee2MQTT (#18388)
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk authored Jul 21, 2023
1 parent df8e168 commit f64cd7e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/extension/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions test/frontend.test.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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}}}));
Expand Down

0 comments on commit f64cd7e

Please sign in to comment.