From e95befcc1c7ddd707639946b220b367089d9ec19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Sat, 7 Dec 2013 23:53:27 +0000 Subject: Better error handling. If any exception is thrown by the library, print an error statement and return a non-zero exit value. --- glucometer.py | 44 +++++++++++++++++++------------------ glucometerutils/drivers/otultra2.py | 2 +- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/glucometer.py b/glucometer.py index d9fbf68..18b7ad3 100755 --- a/glucometer.py +++ b/glucometer.py @@ -14,6 +14,7 @@ import sys from dateutil import parser as date_parser from glucometerutils import common +from glucometerutils import exceptions from glucometerutils.drivers import otultra2 def main(): @@ -49,29 +50,30 @@ def main(): driver = importlib.import_module('glucometerutils.drivers.' + args.driver) device = driver.Device(args.device) - if args.action == 'info': - print(device.get_information_string()) - elif args.action == 'dump': - for reading in device.get_readings(): - print('%s,%.2f,%s' % (reading.timestamp, reading.get_value_as(args.unit), - reading.comment)) - elif args.action == 'datetime': - if args.set == 'now': - print(device.set_datetime()) - elif args.set: - try: - print(device.set_datetime(date_parser.parse(args.set))) - except ValueError: - print('%s: not a valid date' % args.set, file=sys.stderr) - else: - print(device.get_datetime()) - elif args.action == 'zero': - try: + try: + if args.action == 'info': + print(device.get_information_string()) + elif args.action == 'dump': + for reading in device.get_readings(): + print('%s,%.2f,%s' % (reading.timestamp, reading.get_value_as(args.unit), + reading.comment)) + elif args.action == 'datetime': + if args.set == 'now': + print(device.set_datetime()) + elif args.set: + try: + print(device.set_datetime(date_parser.parse(args.set))) + except ValueError: + print('%s: not a valid date' % args.set, file=sys.stderr) + else: + print(device.get_datetime()) + elif args.action == 'zero': device.zero_log() print('Device data log zeroed.') - except Exception as e: - print('Error while zeroing device log: %s' % e) - else: + else: + return 1 + except exceptions.Error as err: + print('Error while executing \'%s\': %s' % (args.action, str(err))) return 1 if __name__ == "__main__": diff --git a/glucometerutils/drivers/otultra2.py b/glucometerutils/drivers/otultra2.py index 4f275ba..500ce17 100644 --- a/glucometerutils/drivers/otultra2.py +++ b/glucometerutils/drivers/otultra2.py @@ -110,7 +110,7 @@ class Device(object): response = self._send_oneliner_command('DM?') if response[0] != '?': - raise InvalidResponse(response) + raise exceptions.InvalidResponse(response) return response[1:] -- cgit v1.2.3