Skip to content

Commit

Permalink
fix(bluetooth-classic): handle exceptions more elegantly
Browse files Browse the repository at this point in the history
Locked adapters throw an exception when another interaction is
requested. Instead of letting the error bubble up we properly catch it
now.
  • Loading branch information
mKeRix committed Oct 31, 2020
1 parent ace9be2 commit 241a251
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Expand Up @@ -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);
Expand Down
15 changes: 11 additions & 4 deletions src/integrations/bluetooth-classic/bluetooth-classic.service.ts
Expand Up @@ -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);
Expand Down

0 comments on commit 241a251

Please sign in to comment.