Skip to content

Commit ba3431d

Browse files
authored
Merge pull request #124 from robberwick/release/udev-rule
feat: enhance USB backend error handling with specific permission exception
2 parents 774e041 + f908a2c commit ba3431d

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

src/blinkstick/backends/unix_like.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,29 @@ def get_attached_blinkstick_devices(
5050
raise USBBackendNotAvailable(
5151
"Could not find USB backend. Is libusb installed?"
5252
)
53-
return [
54-
# TODO: refactor this to DRY up the usb.util.get_string calls
55-
BlinkStickDevice(
56-
raw_device=device,
57-
serial_details=SerialDetails(
58-
serial=str(usb.util.get_string(device, 3, 1033))
59-
),
60-
manufacturer=str(usb.util.get_string(device, 1, 1033)),
61-
version_attribute=device.bcdDevice,
62-
description=str(usb.util.get_string(device, 2, 1033)),
63-
)
64-
for device in raw_devices
65-
]
53+
54+
try:
55+
devices = [
56+
# TODO: refactor this to DRY up the usb.util.get_string calls
57+
BlinkStickDevice(
58+
raw_device=device,
59+
serial_details=SerialDetails(
60+
serial=str(usb.util.get_string(device, 3, 1033))
61+
),
62+
manufacturer=str(usb.util.get_string(device, 1, 1033)),
63+
version_attribute=device.bcdDevice,
64+
description=str(usb.util.get_string(device, 2, 1033)),
65+
)
66+
for device in raw_devices
67+
]
68+
except usb.core.USBError as e:
69+
# if the error is due to a permission issue, raise a more specific exception
70+
if "Operation not permitted" in str(e):
71+
raise USBBackendNotAvailable(
72+
"Permission denied accessing USB backend. Does a udev rule need to be added?"
73+
)
74+
raise
75+
return devices
6676

6777
@staticmethod
6878
def find_by_serial(serial: str) -> list[BlinkStickDevice[usb.core.Device]] | None:

0 commit comments

Comments
 (0)