Kamakitest error msgs, error-handling bugfixes
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Fri, 7 Dec 2012 15:43:01 +0000 (17:43 +0200)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Fri, 7 Dec 2012 15:43:01 +0000 (17:43 +0200)
kamaki/cli/commands/test_cli.py
kamaki/cli/errors.py

index 4acf2d2..8274684 100644 (file)
@@ -54,6 +54,20 @@ class _test_init(_command_init):
 
 
 @command(test_cmds)
+class test_error(_test_init):
+    """Create an error message with optional message"""
+
+    def main(self, errmsg='', importance=0, index=0):
+        from kamaki.cli.errors import raiseCLIError
+        l = [1, 2]
+        try:
+            l[int(index)]
+        except Exception as err:
+            raiseCLIError(err, errmsg, importance)
+        raiseCLIError(None, errmsg, importance)
+
+
+@command(test_cmds)
 class test_args(_test_init):
     """Test how arguments are treated by kamaki"""
 
index 365525b..8fa8137 100644 (file)
@@ -93,7 +93,11 @@ def raiseCLIError(err, message='', importance=0, details=[]):
     """
     from traceback import format_stack
     stack = ['%s' % type(err)] if err else ['<kamaki.cli.errors.CLIError>']
-    recvlog.debug('\n'.join(stack + format_stack()))
+    stack += format_stack()
+    try:
+        stack = [e for e in stack if e != stack[1]]
+    except KeyError:
+        recvlog.debug('\n   < '.join(stack))
 
     if details and not isinstance(details, list):
         details = ['%s' % details]
@@ -102,23 +106,23 @@ def raiseCLIError(err, message='', importance=0, details=[]):
         origerr = '%s' % err
         origerr = origerr if origerr else '%s' % type(err)
     else:
-        origerr = stack
+        origerr = stack[0]
 
-    if message:
-        details.append(origerr)
-    else:
-        message = origerr
+    message = message if message else stack[0]
 
     try:
         status = err.status
     except AttributeError:
         status = None
 
+    if origerr not in details + [message]:
+        details.append(origerr)
     try:
         details.append(err.details)
     except AttributeError:
         pass
 
+    message += '' if message and message[-1] == '\n' else '\n'
     if status:
         message = '(%s) %s' % (err.status, message)
         try: