One more exception and clearer exception messages
[mirror/userdir-ldap.git] / userdir_exceptions.py
1 # vim: set fileencoding=utf-8 ai et sts=4 sw=4 tw=0:
2 # # -*- coding: <utf-8> -*-
3 ## Userdir-LDAP exception classes 
4 ## © 2009 Stephen Gran <sgran@debian.org>
5 ## © 2009 Mark Hymers <mhy@debian.org>
6 """
7 These classes implement the necessary exceptions in the userdir-ldap namespace
8 """
9
10 class UDError(Exception):
11     """
12     Base class for exceptions in ud-ldap.
13     """
14     def __init__(self, message):
15         Exception.__init__(self)
16         self.message = message
17     
18     def __str__(self):
19         return "%s: %s" % (self._name_, self.message)
20
21 __all__ = ['UDError']
22
23 UDERRORS = {
24     "UDPasswdError": """Exception raised for authentication errors.""",
25     "UDFormatError": """Exception raised for data format errors.""",
26     "UDExecuteError": """Exception raised for subprocess execution errors.""",
27     "UDNotAllowedError": """Exception raised for attempts to modify off-limits or disabled entries.""",
28     "UDEmptyList": """Exception raised for empty list objects.""",
29     "UDLoadFail": """Exception raised for LDAP lookup failures.""",
30 }
31
32 def construct_udld_exception(name, description):
33     """Generator function for userdir-ldap exceptions"""
34
35     class Error(UDError):
36         """meta class for user-ldap exceptions"""
37         __doc__ = description
38
39     setattr(Error, "__name__", name)
40     setattr(Error, "_name_", name)
41     return Error
42
43 for key in UDERRORS.keys():
44     globals()[key] = construct_udld_exception(key, UDERRORS[key])
45     __all__ += [key]
46