From dde36950b5d08f2a00a9aeb5c460b40009993a1c Mon Sep 17 00:00:00 2001 From: Rob Berwick Date: Sun, 23 Feb 2025 08:52:00 +0000 Subject: [PATCH 1/2] feat: add USBBackendNotAvailable exception for improved error handling --- src/blinkstick/exceptions.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/blinkstick/exceptions.py b/src/blinkstick/exceptions.py index 86f6f51..1cc2d84 100644 --- a/src/blinkstick/exceptions.py +++ b/src/blinkstick/exceptions.py @@ -7,3 +7,7 @@ class BlinkStickException(Exception): class NotConnected(BlinkStickException): pass + + +class USBBackendNotAvailable(BlinkStickException): + pass From db29e7ee902a1e7c1bec659c9a39a937b5cf60f7 Mon Sep 17 00:00:00 2001 From: Rob Berwick Date: Sun, 23 Feb 2025 08:52:11 +0000 Subject: [PATCH 2/2] feat: handle USB backend errors by raising USBBackendNotAvailable exception --- src/blinkstick/backends/unix_like.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/blinkstick/backends/unix_like.py b/src/blinkstick/backends/unix_like.py index 052d9f1..db6179f 100644 --- a/src/blinkstick/backends/unix_like.py +++ b/src/blinkstick/backends/unix_like.py @@ -6,7 +6,7 @@ from blinkstick.constants import VENDOR_ID, PRODUCT_ID from blinkstick.backends.base import BaseBackend from blinkstick.devices import BlinkStickDevice -from blinkstick.exceptions import BlinkStickException +from blinkstick.exceptions import BlinkStickException, USBBackendNotAvailable from blinkstick.models import SerialDetails @@ -38,10 +38,18 @@ def _refresh_attached_blinkstick_device(self): def get_attached_blinkstick_devices( find_all: bool = True, ) -> list[BlinkStickDevice[usb.core.Device]]: - raw_devices = ( - usb.core.find(find_all=find_all, idVendor=VENDOR_ID, idProduct=PRODUCT_ID) - or [] - ) + try: + raw_devices = ( + usb.core.find( + find_all=find_all, idVendor=VENDOR_ID, idProduct=PRODUCT_ID + ) + or [] + ) + except usb.core.NoBackendError: + # TODO: improve this error message to provide more information on how to remediate the problem + raise USBBackendNotAvailable( + "Could not find USB backend. Is libusb installed?" + ) return [ # TODO: refactor this to DRY up the usb.util.get_string calls BlinkStickDevice(