]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
cephfs-shell: make onecmd() print proper error msg 46110/head
authordparmar18 <dparmar@redhat.com>
Mon, 2 May 2022 11:03:30 +0000 (16:33 +0530)
committerdparmar18 <dparmar@redhat.com>
Mon, 9 May 2022 13:49:39 +0000 (19:19 +0530)
Rationale: Whenever a python exception occurred in cephfs-shell,
           it would often only be the exception message but doesn't
           say anything about the type of exception. For example if
           `ZeroDivisionError: division by zero` occurred, the onecmd()
           would print `division by zero` but will omit the type of
           exception. In this case it's easy to understand but let's
           say an `KeyError` exception occurred for a key `9999` which
           is not existent in the dictionary, onecmd() would print
           just `9999` in this scenario and it would be very difficult
           to interpret what type of error it is.

Fixes: https://tracker.ceph.com/issues/55536
Signed-off-by: Dhairya Parmar <dparmar@redhat.com>
src/tools/cephfs/cephfs-shell

index 1902521325cb023434a4cbdb21c440b9ae61906f..09c7b2d7e297367a243d0bc5432be640aa7f102b 100755 (executable)
@@ -18,6 +18,7 @@ import errno
 
 from cmd2 import Cmd
 from cmd2 import __version__ as cmd2_version
+from cmd2.exceptions import Cmd2ArgparseError
 from distutils.version import LooseVersion
 
 if sys.version_info.major < 3:
@@ -466,7 +467,13 @@ class CephFSShell(Cmd):
         except (libcephfs.Error, Exception) as e:
             if shell.debug:
                 traceback.print_exc(file=sys.stdout)
-            set_exit_code_msg(msg=e)
+            if isinstance(e, Cmd2ArgparseError):
+                # NOTE: In case of Cmd2ArgparseError the error message is
+                # already printed beforehand (plus Cmd2ArgparseError
+                # instances have empty message)
+                pass
+            else:
+                set_exit_code_msg(msg=f'{type(e).__name__}: {e}')
 
     class path_to_bytes(argparse.Action):
         def __call__(self, parser, namespace, values, option_string=None):