Skip to content

Commit

Permalink
Refactor errors to be propagated correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Aug 7, 2023
1 parent c8d7e47 commit dc6cc14
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/usb-device-webusb.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,49 +62,50 @@ class UsbDevice {
throw new UsbError('Unable to close USB device', { cause: err });
}
}

async transferIn(setup) {
let res;
try {
const res = await this._dev.controlTransferIn({
res = await this._dev.controlTransferIn({
requestType: bmRequestTypeToString(setup.bmRequestType),
recipient: bmRequestTypeToRecipientString(setup.bmRequestType),
request: setup.bRequest,
value: setup.wValue,
index: setup.wIndex
}, setup.wLength);
if (res.status !== 'ok') {
if (res.status === 'stall') {
throw new UsbStallError(`Status: ${res.status}`);
}
throw new Error(`Status: ${res.status}`);
}
return Buffer.from(res.data.buffer);
} catch (err) {
throw new UsbError('IN control transfer failed', { cause: err });
}
if (res.status !== 'ok') {
if (res.status === 'stall') {
throw new UsbStallError('Transfer stalled');
}
throw new Error(`Status: ${res.status}`);
}
return Buffer.from(res.data.buffer);
}

async transferOut(setup, data) {
let res;
try {
if (!data && this._quirks.controlOutTransfersRequireDataStage) {
data = Buffer.alloc(1);
}
const res = await this._dev.controlTransferOut({
res = await this._dev.controlTransferOut({
requestType: bmRequestTypeToString(setup.bmRequestType),
recipient: bmRequestTypeToRecipientString(setup.bmRequestType),
request: setup.bRequest,
value: setup.wValue,
index: setup.wIndex
}, data); // data is optional
if (res.status !== 'ok') {
if (res.status === 'stall') {
throw new UsbStallError(`Status: ${res.status}`);
}
throw new Error(`Status: ${res.status}`);
}
} catch (err) {
throw new UsbError('OUT control transfer failed', { cause: err });
}
if (res.status !== 'ok') {
if (res.status === 'stall') {
throw new UsbStallError('Transfer stalled');
}
throw new Error(`Status: ${res.status}`);
}
}

async claimInterface(intrface) {
Expand Down

0 comments on commit dc6cc14

Please sign in to comment.