Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Error Handling for Device Mode Checks #715

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 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) {
throw new Error('Operation could not be completed due to device protection.');
} else {
throw new Error(`Unable to get device mode: ${err.message}`);
}
Expand Down Expand Up @@ -362,5 +365,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;
}
throw error;
})
Expand Down