From 241a251a17adf2c2a4d37d75b9cc49a1e026a297 Mon Sep 17 00:00:00 2001 From: Heiko Rothe Date: Sat, 31 Oct 2020 10:18:00 +0100 Subject: [PATCH] fix(bluetooth-classic): handle exceptions more elegantly Locked adapters throw an exception when another interaction is requested. Instead of letting the error bubble up we properly catch it now. --- .../bluetooth-classic.service.spec.ts | 17 +++++++++++++++++ .../bluetooth-classic.service.ts | 15 +++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/integrations/bluetooth-classic/bluetooth-classic.service.spec.ts b/src/integrations/bluetooth-classic/bluetooth-classic.service.spec.ts index c1a73e6d..a291da42 100644 --- a/src/integrations/bluetooth-classic/bluetooth-classic.service.spec.ts +++ b/src/integrations/bluetooth-classic/bluetooth-classic.service.spec.ts @@ -205,6 +205,23 @@ describe('BluetoothClassicService', () => { expect(handleRssiMock).not.toHaveBeenCalled(); }); + it('should not publish an RSSI value if an error occured', async () => { + jest.spyOn(service, 'shouldInquire').mockReturnValue(true); + bluetoothService.inquireClassicRssi.mockRejectedValue( + new Error('expected for this test') + ); + const handleRssiMock = jest + .spyOn(service, 'handleNewRssi') + .mockImplementation(() => undefined); + + await expect( + service.handleRssiRequest('77:50:fb:4d:ab:70') + ).resolves.not.toThrow(); + + expect(clusterService.publish).not.toHaveBeenCalled(); + expect(handleRssiMock).not.toHaveBeenCalled(); + }); + it('should publish RSSI values that are bigger than the min RSSI', async () => { jest.spyOn(service, 'shouldInquire').mockReturnValue(true); bluetoothService.inquireClassicRssi.mockResolvedValue(-9); diff --git a/src/integrations/bluetooth-classic/bluetooth-classic.service.ts b/src/integrations/bluetooth-classic/bluetooth-classic.service.ts index 90e786c9..c19569dd 100644 --- a/src/integrations/bluetooth-classic/bluetooth-classic.service.ts +++ b/src/integrations/bluetooth-classic/bluetooth-classic.service.ts @@ -101,10 +101,17 @@ export class BluetoothClassicService } if (this.shouldInquire()) { - let rssi = await this.bluetoothService.inquireClassicRssi( - this.config.hciDeviceId, - address - ); + let rssi; + try { + rssi = await this.bluetoothService.inquireClassicRssi( + this.config.hciDeviceId, + address + ); + } catch (e) { + this.logger.error( + `Failed to retrieve RSSI for ${address}: ${e.message}` + ); + } if (rssi !== undefined) { rssi = _.round(this.filterRssi(address, rssi), 1);