Refactor config sys error handler 37/13237/3
authorThanh Ha <thanh.ha@linuxfoundation.org>
Mon, 29 Oct 2018 23:42:59 +0000 (19:42 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Tue, 30 Oct 2018 00:16:20 +0000 (20:16 -0400)
Use exception system to allow downstream users to raise their
own exceptions.

Change-Id: I184eadf93f101cbe96ea5a1e10406dac6c906767
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
lftools/cli/config.py
lftools/config.py

index ad08849..29e2442 100644 (file)
 __author__ = 'Thanh Ha'
 
 import logging
+import sys
 
 import click
+from six.moves import configparser
 
 from lftools import config
 
@@ -33,7 +35,13 @@ def config_sys(ctx):
 @click.pass_context
 def get_setting(ctx, section, option):
     """Print section or setting from config file."""
-    result = config.get_setting(section, option)
+    try:
+        result = config.get_setting(section, option)
+    except (configparser.NoOptionError,
+            configparser.NoSectionError) as e:
+        log.error(e)
+        sys.exit(1)
+
     if isinstance(result, list):
         for i in result:
             log.info('{}: {}'.format(i, config.get_setting(section, i)))
index e4d94b5..4b969be 100644 (file)
@@ -13,7 +13,6 @@
 __author__ = 'Thanh Ha'
 
 import logging
-import sys
 
 from six.moves import configparser
 from xdg import XDG_CONFIG_HOME
@@ -36,20 +35,15 @@ def get_setting(section, option=None):
     if option:
         try:
             return config.get(section, option)
-        except configparser.NoOptionError:
-            print(section, option)
-            log.error('Config option does not exist.')
-            sys.exit(1)
-        except configparser.NoSectionError:
-            log.error('Config section does not exist.')
-            sys.exit(1)
+        except (configparser.NoOptionError,
+                configparser.NoSectionError) as e:
+            raise e
 
     else:
         try:
             return config.options(section)
-        except configparser.NoSectionError:
-            log.error('Config section does not exist.')
-            sys.exit(1)
+        except configparser.NoSectionError as e:
+            raise e
 
 
 def set_setting(section, option, value):