VMM/cli/main: Except more errors and handle them correct. v0.6.x
authorPascal Volk <neverseen@users.sourceforge.net>
Fri, 06 Aug 2010 03:45:34 +0000
branchv0.6.x
changeset 343 9232ed7e4d85
parent 342 b0c971f943dc
child 344 0d2430dc6ef8
VMM/cli/main: Except more errors and handle them correct.
VirtualMailManager/cli/main.py
--- a/VirtualMailManager/cli/main.py	Fri Aug 06 02:15:16 2010 +0000
+++ b/VirtualMailManager/cli/main.py	Fri Aug 06 03:45:34 2010 +0000
@@ -8,25 +8,31 @@
     VirtualMailManager's command line interface.
 """
 
+from ConfigParser import NoOptionError, NoSectionError
+
 from VirtualMailManager import ENCODING, errors
-from VirtualMailManager.config import ConfigValueError
+from VirtualMailManager.config import BadOptionError, ConfigValueError
 from VirtualMailManager.cli import w_err
 from VirtualMailManager.cli.handler import CliHandler
-from VirtualMailManager.constants import DATABASE_ERROR, EX_MISSING_ARGS, \
-     EX_SUCCESS, EX_UNKNOWN_COMMAND, EX_USER_INTERRUPT
+from VirtualMailManager.constants import CONF_ERROR, DATABASE_ERROR, \
+     EX_MISSING_ARGS, EX_SUCCESS, EX_UNKNOWN_COMMAND, EX_USER_INTERRUPT, \
+     INVALID_ARGUMENT
 from VirtualMailManager.cli.subcommands import RunContext, cmd_map, usage
 
 
 _ = lambda msg: msg
 
+
 def _get_handler():
     """Try to get a CliHandler. Exit the program when an error occurs."""
     try:
         handler = CliHandler()
         handler.cfg_install()
     except (errors.NotRootError, errors.PermissionError, errors.VMMError,
-            errors.ConfigError, ConfigValueError), err:
+            errors.ConfigError), err:
         w_err(err.code, _(u'Error: %s') % err.msg)
+    except ConfigValueError, err:
+        w_err(CONF_ERROR, _(u'Error: %s') % err)
     else:
         return handler
 
@@ -59,6 +65,14 @@
         if err.code != DATABASE_ERROR:
             w_err(err.code, _(u'Error: %s') % err.msg)
         w_err(err.code, unicode(err.msg, ENCODING, 'replace'))
+    except (BadOptionError, ConfigValueError), err:
+        w_err(INVALID_ARGUMENT, _(u'Error: %s') % err)
+    except NoSectionError, err:
+        w_err(INVALID_ARGUMENT, _(u"Error: No section '%s'") % err.section)
+    except NoOptionError, err:
+        w_err(INVALID_ARGUMENT,
+              _(u"Error: No option '%(option)s' in section: '%(section)s'") %
+              {'option': err.option, 'section': err.section})
     if handler.has_warnings():
         w_err(0, _(u'Warnings:'), *handler.get_warnings())
     return EX_SUCCESS