summaryrefslogtreecommitdiffstats
path: root/ipa-python
diff options
context:
space:
mode:
authorKevin McCarthy <kmccarth@redhat.com>2007-08-22 10:30:51 -0700
committerKevin McCarthy <kmccarth@redhat.com>2007-08-22 10:30:51 -0700
commit7691653c0a3ee95aab7c64d631c694a300547345 (patch)
treed1d3018438cc2b4abdc0b24c2751b85028e96e76 /ipa-python
parenta8f302aa9f193984d68318a65a51b41298b1391d (diff)
downloadfreeipa-7691653c0a3ee95aab7c64d631c694a300547345.tar.gz
freeipa-7691653c0a3ee95aab7c64d631c694a300547345.tar.xz
freeipa-7691653c0a3ee95aab7c64d631c694a300547345.zip
Create ipaerror module.
Move LDAPError trapping/conversion into the ipaldap module. Fix xmlrpc layer to encode/decode ipaerrors properly. Also, implement mid-air collision exception for updates.
Diffstat (limited to 'ipa-python')
-rw-r--r--ipa-python/ipaerror.py126
-rw-r--r--ipa-python/rpcclient.py15
2 files changed, 134 insertions, 7 deletions
diff --git a/ipa-python/ipaerror.py b/ipa-python/ipaerror.py
new file mode 100644
index 000000000..c637aeef0
--- /dev/null
+++ b/ipa-python/ipaerror.py
@@ -0,0 +1,126 @@
+#! /usr/bin/python -E
+#
+# Copyright (C) 2007 Red Hat
+# see file 'COPYING' for use and warranty information
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; version 2 or later
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+import exceptions
+import types
+
+class IPAError(exceptions.Exception):
+ """Base error class for IPA Code"""
+
+ def __init__(self, code, message="", detail=None):
+ """code is the IPA error code.
+ message is a human viewable error message.
+ detail is an optional exception that provides more detail about the
+ error."""
+ self.code = code
+ self.message = message
+ self.detail = detail
+
+ def __str__(self):
+ return self.message
+
+ def __repr__(self):
+ repr = "%d: %s" % (self.code, self.message)
+ if self.detail:
+ repr += "\n%s" % str(self.detail)
+ return repr
+
+
+###############
+# Error codes #
+###############
+
+code_map_dict = {}
+
+def gen_exception(code, message=None, nested_exception=None):
+ """This should be used by IPA code to translate error codes into the
+ correct exception/message to throw.
+
+ message is an optional argument which overrides the default message.
+
+ nested_exception is an optional argument providing more details
+ about the error."""
+ (default_message, exception) = code_map_dict.get(code, ("unknown", IPAError))
+ if not message:
+ message = default_message
+ return exception(code, message, nested_exception)
+
+def exception_for(code):
+ """Used to look up the corresponding exception for an error code.
+ Will usually be used for an except block."""
+ (default_message, exception) = code_map_dict.get(code, ("unknown", IPAError))
+ return exception
+
+def gen_error_code(category, detail, message):
+ """Private method used to generate exception codes.
+ category is one of the 16 bit error code category constants.
+ detail is a 16 bit code within the category.
+ message is a human readable description on the error.
+ exception is the exception to throw for this error code."""
+ code = (category << 16) + detail
+ exception = types.ClassType("IPAError%d" % code,
+ (IPAError,),
+ {})
+ code_map_dict[code] = (message, exception)
+
+ return code
+
+#
+# Error codes are broken into two 16-bit values: category and detail
+#
+
+#
+# LDAP Errors: 0x0001
+#
+LDAP_CATEGORY = 0x0001
+
+LDAP_DATABASE_ERROR = gen_error_code(
+ LDAP_CATEGORY,
+ 0x0001,
+ "A database error occurred")
+
+LDAP_MIDAIR_COLLISION = gen_error_code(
+ LDAP_CATEGORY,
+ 0x0002,
+ "Change collided with another change")
+
+LDAP_NOT_FOUND = gen_error_code(
+ LDAP_CATEGORY,
+ 0x0003,
+ "Entry not found")
+
+LDAP_DUPLICATE = gen_error_code(
+ LDAP_CATEGORY,
+ 0x0004,
+ "Duplicate entry already in LDAP")
+
+LDAP_MISSING_DN = gen_error_code(
+ LDAP_CATEGORY,
+ 0x0005,
+ "Entry missing dn")
+
+#
+# Input errors (sample - replace me)
+#
+INPUT_CATEGORY = 0x0002
+
+INPUT_INVALID_ERROR = gen_error_code(
+ INPUT_CATEGORY,
+ 0x0001,
+ "Illegal input")
diff --git a/ipa-python/rpcclient.py b/ipa-python/rpcclient.py
index 477b4e3cf..d5a2d4262 100644
--- a/ipa-python/rpcclient.py
+++ b/ipa-python/rpcclient.py
@@ -29,6 +29,7 @@ import os
import base64
import user
import ipa
+from ipa import ipaerror
# Some errors to catch
# http://cvs.fedora.redhat.com/viewcvs/ldapserver/ldap/servers/plugins/pam_passthru/README?root=dirsec&rev=1.6&view=auto
@@ -76,7 +77,7 @@ class RPCClient:
else:
result = server.get_user(username)
except xmlrpclib.Fault, fault:
- raise xmlrpclib.Fault(fault.faultCode, fault.faultString)
+ raise ipaerror.gen_exception(fault.faultCode, fault.faultString)
except socket.error, (value, msg):
raise xmlrpclib.Fault(value, msg)
@@ -91,7 +92,7 @@ class RPCClient:
try:
result = server.add_user(user)
except xmlrpclib.Fault, fault:
- raise xmlrpclib.Fault(fault.faultCode, fault.faultString)
+ raise ipaerror.gen_exception(fault.faultCode, fault.faultString)
except socket.error, (value, msg):
raise xmlrpclib.Fault(value, msg)
@@ -108,7 +109,7 @@ class RPCClient:
try:
result = server.get_add_schema()
except xmlrpclib.Fault, fault:
- raise xmlrpclib.Fault(fault.faultCode, fault.faultString)
+ raise ipaerror.gen_exception(fault.faultCode, fault.faultString)
except socket.error, (value, msg):
raise xmlrpclib.Fault(value, msg)
@@ -121,7 +122,7 @@ class RPCClient:
try:
result = server.get_all_users()
except xmlrpclib.Fault, fault:
- raise xmlrpclib.Fault(fault.faultCode, fault.faultString)
+ raise ipaerror.gen_exception(fault.faultCode, fault.faultString)
except socket.error, (value, msg):
raise xmlrpclib.Fault(value, msg)
@@ -138,7 +139,7 @@ class RPCClient:
else:
result = server.find_users(criteria)
except xmlrpclib.Fault, fault:
- raise xmlrpclib.Fault(fault.faultCode, fault.faultString)
+ raise ipaerror.gen_exception(fault.faultCode, fault.faultString)
except socket.error, (value, msg):
raise xmlrpclib.Fault(value, msg)
@@ -151,7 +152,7 @@ class RPCClient:
try:
result = server.update_user(olduser, newuser)
except xmlrpclib.Fault, fault:
- raise xmlrpclib.Fault(fault.faultCode, fault.faultString)
+ raise ipaerror.gen_exception(fault.faultCode, fault.faultString)
except socket.error, (value, msg):
raise xmlrpclib.Fault(value, msg)
@@ -164,7 +165,7 @@ class RPCClient:
try:
result = server.mark_user_deleted(uid)
except xmlrpclib.Fault, fault:
- raise xmlrpclib.Fault(fault.faultCode, fault.faultString)
+ raise ipaerror.gen_exception(fault.faultCode, fault.faultString)
except socket.error, (value, msg):
raise xmlrpclib.Fault(value, msg)