summaryrefslogtreecommitdiffstats
path: root/ipapython/ipautil.py
diff options
context:
space:
mode:
Diffstat (limited to 'ipapython/ipautil.py')
-rw-r--r--ipapython/ipautil.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
index fbb3c26d8..9fdd2fd76 100644
--- a/ipapython/ipautil.py
+++ b/ipapython/ipautil.py
@@ -1174,3 +1174,39 @@ def restore_hostname(statestore):
run(['/bin/hostname', old_hostname])
except CalledProcessError, e:
print >>sys.stderr, "Failed to set this machine hostname back to %s: %s" % (old_hostname, str(e))
+
+def convert_ldap_error(exc):
+ """
+ Make LDAP exceptions prettier.
+
+ Some LDAP exceptions have a dict with descriptive information, if
+ this exception has a dict extract useful information from it and
+ format it into something usable and return that. If the LDAP
+ exception does not have an information dict then return the name
+ of the LDAP exception.
+
+ If the exception is not an LDAP exception then convert the
+ exception to a string and return that instead.
+ """
+ if isinstance(exc, ldap.LDAPError):
+ name = exc.__class__.__name__
+
+ if len(exc.args):
+ d = exc.args[0]
+ if isinstance(d, dict):
+ desc = d.get('desc', '').strip()
+ info = d.get('info', '').strip()
+ if desc and info:
+ return '%s %s' % (desc, info)
+ elif desc:
+ return desc
+ elif info:
+ return info
+ else:
+ return name
+ else:
+ return name
+ else:
+ return name
+ else:
+ return str(exc)