This is an automated email from the git hooks/post-receive script.
spichugi pushed a commit to branch 389-ds-base-1.4.1
in repository 389-ds-base.
commit 49eef87e53a91ac9f0a7ed32d3bd1d198c90a6a2
Author: Simon Pichugin <spichugi(a)redhat.com>
AuthorDate: Thu Oct 17 13:46:56 2019 +0200
Issue 50634 - Clean up CLI errors output
Description: CLI tools should print human easy readable messages
if something went wrong.
As discussed here:
https://pagure.io/389-ds-base/pull-request/50624
Change the CLI error processing so the dict type is always transformed.
https://pagure.io/389-ds-base/issue/50634
Reviewed by: mreynolds, mhonek (Thanks!)
---
src/lib389/cli/dsconf | 11 ++++-------
src/lib389/cli/dscreate | 4 +++-
src/lib389/cli/dsctl | 11 +++++++----
src/lib389/cli/dsidm | 4 +++-
src/lib389/lib389/cli_base/__init__.py | 20 ++++++++++++++++++++
5 files changed, 37 insertions(+), 13 deletions(-)
diff --git a/src/lib389/cli/dsconf b/src/lib389/cli/dsconf
index 1940b8e..2059a06 100755
--- a/src/lib389/cli/dsconf
+++ b/src/lib389/cli/dsconf
@@ -15,7 +15,6 @@ import ldap
import sys
import signal
import json
-import ast
from lib389._constants import DSRC_HOME
from lib389.cli_conf import config as cli_config
from lib389.cli_conf import backend as cli_backend
@@ -34,6 +33,7 @@ from lib389.cli_conf import security as cli_security
from lib389.cli_base import disconnect_instance, connect_instance
from lib389.cli_base.dsrc import dsrc_to_ldap, dsrc_arg_concat
from lib389.cli_base import setup_script_logger
+from lib389.cli_base import format_error_to_dict
parser = argparse.ArgumentParser(allow_abbrev=True)
@@ -138,15 +138,12 @@ if __name__ == '__main__':
log.info("Command successful.")
except Exception as e:
log.debug(e, exc_info=True)
- errmsg = str(e)
+ msg = format_error_to_dict(e)
+
if args and args.json:
- try:
- msg = ast.literal_eval(errmsg)
- except:
- msg = {'desc': errmsg}
sys.stderr.write(json.dumps(msg))
else:
- log.error("Error: %s" % errmsg)
+ log.error("Error: %s" % " - ".join(msg.values()))
result = False
disconnect_instance(inst)
diff --git a/src/lib389/cli/dscreate b/src/lib389/cli/dscreate
index 4af852b..b516eb9 100755
--- a/src/lib389/cli/dscreate
+++ b/src/lib389/cli/dscreate
@@ -17,6 +17,7 @@ import signal
from lib389 import DirSrv
from lib389.cli_ctl import instance as cli_instance
from lib389.cli_base import setup_script_logger
+from lib389.cli_base import format_error_to_dict
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose',
@@ -74,7 +75,8 @@ if __name__ == '__main__':
result = args.func(inst, log, args)
except Exception as e:
log.debug(e, exc_info=True)
- log.error("Error: %s" % str(e))
+ msg = format_error_to_dict(e)
+ log.error("Error: %s" % " - ".join(msg.values()))
result = False
# Done!
diff --git a/src/lib389/cli/dsctl b/src/lib389/cli/dsctl
index e76f3e5..3be9540 100755
--- a/src/lib389/cli/dsctl
+++ b/src/lib389/cli/dsctl
@@ -21,6 +21,7 @@ from lib389 import DirSrv
from lib389.cli_ctl import instance as cli_instance
from lib389.cli_ctl import dbtasks as cli_dbtasks
from lib389.cli_base import disconnect_instance, setup_script_logger
+from lib389.cli_base import format_error_to_dict
from lib389.cli_ctl.instance import instance_remove_all
from lib389._constants import DSRC_CONTAINER
@@ -106,10 +107,12 @@ if __name__ == '__main__':
insts = inst.list(serverid=args.instance)
except (PermissionError, IOError) as e:
log.error("Unable to access instance information. Are you running as the
correct user? (usually dirsrv or root)")
- log.error("Error: %s" % str(e))
+ msg = format_error_to_dict(e)
+ log.error("Error: %s" % " - ".join(msg.values()))
sys.exit(1)
except Exception as e:
- log.error("Error: %s" % str(e))
+ msg = format_error_to_dict(e)
+ log.error("Error: %s" % " - ".join(msg.values()))
sys.exit(1)
if len(insts) != 1:
log.error("No such instance '%s'" % args.instance)
@@ -123,11 +126,11 @@ if __name__ == '__main__':
result = args.func(inst, log, args)
except Exception as e:
log.debug(e, exc_info=True)
- log.error("Error: %s" % str(e))
+ msg = format_error_to_dict(e)
+ log.error("Error: %s" % " - ".join(msg.values()))
result = False
disconnect_instance(inst)
# Done!
if result is False:
sys.exit(1)
-
diff --git a/src/lib389/cli/dsidm b/src/lib389/cli/dsidm
index 7fd77b6..da246b5 100755
--- a/src/lib389/cli/dsidm
+++ b/src/lib389/cli/dsidm
@@ -26,6 +26,7 @@ from lib389.cli_idm import client_config as cli_client_config
from lib389.cli_idm import role as cli_role
from lib389.cli_base import connect_instance, disconnect_instance, setup_script_logger
from lib389.cli_base.dsrc import dsrc_to_ldap, dsrc_arg_concat
+from lib389.cli_base import format_error_to_dict
parser = argparse.ArgumentParser(allow_abbrev=True)
@@ -131,7 +132,8 @@ if __name__ == '__main__':
log.info("Command successful.")
except Exception as e:
log.debug(e, exc_info=True)
- log.error("Error: %s" % str(e))
+ msg = format_error_to_dict(e)
+ log.error("Error: %s" % " - ".join(msg.values()))
result = False
disconnect_instance(inst)
diff --git a/src/lib389/lib389/cli_base/__init__.py
b/src/lib389/lib389/cli_base/__init__.py
index 932938b..23fc312 100644
--- a/src/lib389/lib389/cli_base/__init__.py
+++ b/src/lib389/lib389/cli_base/__init__.py
@@ -7,6 +7,7 @@
# See LICENSE for details.
# --- END COPYRIGHT BLOCK ---
+import ast
import logging
import sys
import json
@@ -406,3 +407,22 @@ def setup_script_logger(name, verbose=False):
root.addHandler(log_handler)
return log
+
+
+def format_error_to_dict(exception):
+ """python-ldap str(exception) processing is not consistent.
+ This function makes sure that the result is dict
+
+ :param exception: Exception you need to print
+ :type exception: ldap.LDAPError
+ :returns: dict
+ """
+
+ # The issue was raise on python-ldap repo:
https://github.com/python-ldap/python-ldap/issues/304
+ # We should fix the code here after the issue is fixed
+ errmsg = str(exception)
+ try:
+ msg = ast.literal_eval(errmsg)
+ except ValueError:
+ msg = {'desc': errmsg}
+ return msg
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.