Skip to content
This repository was archived by the owner on Jun 27, 2022. It is now read-only.

Commit 17903be

Browse files
juan-cortesgre
andauthored
LL-4600 Map HID errors to DisconnectedDeviceDuringOperation (#620)
* LL-4600 Map HID errors to DisconnectedDeviceDuringOperation * Use e.message * Simplify mapping * Missing new 🤦 * Update packages/hw-transport-node-hid-noevents/src/TransportNodeHid.ts Co-authored-by: Gaëtan Renaudeau <[email protected]> * Update packages/hw-transport-node-hid-noevents/src/TransportNodeHid.ts Co-authored-by: Gaëtan Renaudeau <[email protected]> Co-authored-by: Gaëtan Renaudeau <[email protected]>
1 parent 7fd06f7 commit 17903be

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

packages/hw-transport-node-hid-noevents/src/TransportNodeHid.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { ledgerUSBVendorId } from "@ledgerhq/devices";
1010
import hidFraming from "@ledgerhq/devices/lib/hid-framing";
1111
import { identifyUSBProductId, identifyProductName } from "@ledgerhq/devices";
1212
import { DeviceModel } from "@ledgerhq/devices";
13-
import { TransportError, DisconnectedDevice } from "@ledgerhq/errors";
13+
import {
14+
TransportError,
15+
DisconnectedDevice,
16+
DisconnectedDeviceDuringOperation,
17+
} from "@ledgerhq/errors";
1418

1519
const filterInterface = (device) =>
1620
["win32", "darwin"].includes(process.platform)
@@ -21,8 +25,6 @@ export function getDevices(): (Device & { deviceName?: string })[] {
2125
return HID.devices(ledgerUSBVendorId, 0x0).filter(filterInterface);
2226
}
2327

24-
const isDisconnectedError = (e) =>
25-
e && e.message && e.message.indexOf("HID") >= 0;
2628
/**
2729
* node-hid Transport minimal implementation
2830
* @example
@@ -109,12 +111,13 @@ export default class TransportNodeHidNoEvents extends Transport {
109111
this.device.write(data);
110112
return Promise.resolve();
111113
} catch (e) {
112-
if (isDisconnectedError(e)) {
114+
const maybeMappedError =
115+
e && e.message ? new DisconnectedDeviceDuringOperation(e.message) : e;
116+
if (maybeMappedError instanceof DisconnectedDeviceDuringOperation) {
113117
this.setDisconnected();
114-
return Promise.reject(new DisconnectedDevice(e.message));
115118
}
116119

117-
return Promise.reject(e);
120+
return Promise.reject(maybeMappedError);
118121
}
119122
};
120123
readHID = (): Promise<Buffer> =>
@@ -125,12 +128,15 @@ export default class TransportNodeHidNoEvents extends Transport {
125128
}
126129

127130
if (e) {
128-
if (isDisconnectedError(e)) {
131+
const maybeMappedError =
132+
e && e.message
133+
? new DisconnectedDeviceDuringOperation(e.message)
134+
: e;
135+
if (maybeMappedError instanceof DisconnectedDeviceDuringOperation) {
129136
this.setDisconnected();
130-
return reject(new DisconnectedDevice(e.message));
131137
}
132138

133-
reject(e);
139+
reject(maybeMappedError);
134140
} else {
135141
const buffer = Buffer.from(res);
136142
resolve(buffer);

0 commit comments

Comments
 (0)