Skip to content

Commit

Permalink
Merge pull request #715 from particle-iot/fix/throw-correct-error-fro…
Browse files Browse the repository at this point in the history
…m-protected-devices

Add Error Handling for Device Mode Checks
  • Loading branch information
keeramis committed Jan 30, 2024
2 parents df615d7 + fb602a3 commit 9be3030
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/cmd/usb-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const {
openDeviceById,
NotFoundError,
NotAllowedError,
TimeoutError
TimeoutError,
DeviceProtectionError
} = require('../lib/require-optional')('particle-usb');

// Timeout when reopening a USB device after an update via control requests. This timeout should be
Expand Down Expand Up @@ -35,6 +36,8 @@ async function _getDeviceInfo(device) {
} catch (err) {
if (err instanceof TimeoutError) {
return { id, mode: 'UNKNOWN' };
} else if (err instanceof DeviceProtectionError) {
return { id, mode: 'PROTECTED' };
} else {
throw new Error(`Unable to get device mode: ${err.message}`);
}
Expand Down Expand Up @@ -239,6 +242,11 @@ async function getOneUsbDevice({ idOrName, api, auth, ui, flashMode, platformId
usbDevice = devices[0].value;
}

const devInfo = await _getDeviceInfo(usbDevice);
if (devInfo.mode === 'PROTECTED') {
ui.write(`Attempted to flash device ${devInfo.id}`);
throw new Error('Operation could not be completed due to device protection.');
}

try {
await usbDevice.open();
Expand Down Expand Up @@ -362,5 +370,6 @@ module.exports = {
reopenInNormalMode,
reopenDevice,
UsbPermissionsError,
TimeoutError
TimeoutError,
DeviceProtectionError
};
6 changes: 4 additions & 2 deletions src/cmd/usb.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { spin } = require('../app/ui');
const { delay, asyncMapSeries, buildDeviceFilter } = require('../lib/utilities');
const { getDevice, formatDeviceInfo } = require('./device-util');
const { getUsbDevices, openUsbDevice, openUsbDeviceByIdOrName, TimeoutError } = require('./usb-util');
const { getUsbDevices, openUsbDevice, openUsbDeviceByIdOrName, TimeoutError, DeviceProtectionError } = require('./usb-util');
const { systemSupportsUdev, udevRulesInstalled, installUdevRules } = require('./udev');
const { platformForId, isKnownPlatformId } = require('../lib/platform');
const ParticleApi = require('./api');
Expand Down Expand Up @@ -46,8 +46,10 @@ module.exports = class UsbCommand {
info.push(
usbDevice.getDeviceMode({ timeout: 10 * 1000 })
.catch(error => {
if (error instanceof TimeoutError){
if (error instanceof TimeoutError) {
return 'UNKNOWN';
} else if (error instanceof DeviceProtectionError) {
return 'PROTECTED';
}
throw error;
})
Expand Down

0 comments on commit 9be3030

Please sign in to comment.