blinkstick-python icon indicating copy to clipboard operation
blinkstick-python copied to clipboard

Please implement Exceptions for better Error handling

Open henryruhs opened this issue 6 years ago • 1 comments

Hello,

it would be nice to have some exceptions to be thrown:

  1. Connection failed while using blinkstick.connect() should throw BlinkStickConnectionException

Hacky solution that includes general OSError to catch pointless find_all() call:

	api = None

	try:
		from blinkstick import blinkstick

		try:
			api = blinkstick
			api.find_all()
		except OSError:
			exit(wording.get('connection_no').format('AGILE INNOVATIVE BLINKSTICK') + wording.get('exclamation_mark'))
		return api
	except ImportError:
		exit(wording.get('package_no').format('BLINKSTICK') + wording.get('exclamation_mark'))

Wanted solution that throws BlinkStickConnectionException:

def api_factory():
	api = None

	try:
		from blinkstick.blinkstick import BlinkStickConnectionException

		try:
			api = blinkstick
			blinkstick.connect()
		except BlinkStickConnectionException:
			exit(wording.get('connection_no').format('AGILE INNOVATIVE BLINKSTICK') + wording.get('exclamation_mark'))
		return api
	except ImportError:
		exit(wording.get('package_no').format('BLINKSTICK') + wording.get('exclamation_mark'))

Connect method could look similar to this:

def connect(vendor_id : str, product_id : str) -> None:
	try:
		hidraw = hid.device(vendor_id, product_id)
		hidraw.open(vendor_id, product_id)
		hidraw.close()
	except OSError:
		raise BlinkStickConnectionException

henryruhs avatar Nov 24 '19 16:11 henryruhs

I second this. The exceptions being thrown are utterly unhelpful.

rakshak-t avatar Mar 03 '21 08:03 rakshak-t