From f908a2c668bc9bf99a8c58fa0ed02b343f7b9ea6 Mon Sep 17 00:00:00 2001 From: Rob Berwick Date: Sun, 23 Feb 2025 09:56:01 +0000 Subject: [PATCH] feat: enhance USB backend error handling with specific permission exception --- src/blinkstick/backends/unix_like.py | 36 ++++++++++++++++++---------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/blinkstick/backends/unix_like.py b/src/blinkstick/backends/unix_like.py index db6179f..a953b5f 100644 --- a/src/blinkstick/backends/unix_like.py +++ b/src/blinkstick/backends/unix_like.py @@ -50,19 +50,29 @@ def get_attached_blinkstick_devices( raise USBBackendNotAvailable( "Could not find USB backend. Is libusb installed?" ) - return [ - # TODO: refactor this to DRY up the usb.util.get_string calls - BlinkStickDevice( - raw_device=device, - serial_details=SerialDetails( - serial=str(usb.util.get_string(device, 3, 1033)) - ), - manufacturer=str(usb.util.get_string(device, 1, 1033)), - version_attribute=device.bcdDevice, - description=str(usb.util.get_string(device, 2, 1033)), - ) - for device in raw_devices - ] + + try: + devices = [ + # TODO: refactor this to DRY up the usb.util.get_string calls + BlinkStickDevice( + raw_device=device, + serial_details=SerialDetails( + serial=str(usb.util.get_string(device, 3, 1033)) + ), + manufacturer=str(usb.util.get_string(device, 1, 1033)), + version_attribute=device.bcdDevice, + description=str(usb.util.get_string(device, 2, 1033)), + ) + for device in raw_devices + ] + except usb.core.USBError as e: + # if the error is due to a permission issue, raise a more specific exception + if "Operation not permitted" in str(e): + raise USBBackendNotAvailable( + "Permission denied accessing USB backend. Does a udev rule need to be added?" + ) + raise + return devices @staticmethod def find_by_serial(serial: str) -> list[BlinkStickDevice[usb.core.Device]] | None: