From c5f3c5122c7d3e23edce8798fcb5a25d4d6f527e Mon Sep 17 00:00:00 2001 From: dparmar18 Date: Mon, 2 May 2022 16:33:30 +0530 Subject: [PATCH] cephfs-shell: make onecmd() print proper error msg 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 --- src/tools/cephfs/cephfs-shell | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/tools/cephfs/cephfs-shell b/src/tools/cephfs/cephfs-shell index 1902521325cb0..09c7b2d7e2973 100755 --- a/src/tools/cephfs/cephfs-shell +++ b/src/tools/cephfs/cephfs-shell @@ -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):