Package dbus :: Module exceptions
[hide private]
[frames] | no frames]

Source Code for Module dbus.exceptions

  1  """D-Bus exceptions.""" 
  2   
  3  # Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/> 
  4  # 
  5  # Permission is hereby granted, free of charge, to any person 
  6  # obtaining a copy of this software and associated documentation 
  7  # files (the "Software"), to deal in the Software without 
  8  # restriction, including without limitation the rights to use, copy, 
  9  # modify, merge, publish, distribute, sublicense, and/or sell copies 
 10  # of the Software, and to permit persons to whom the Software is 
 11  # furnished to do so, subject to the following conditions: 
 12  # 
 13  # The above copyright notice and this permission notice shall be 
 14  # included in all copies or substantial portions of the Software. 
 15  # 
 16  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 17  # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 18  # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 19  # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 20  # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 21  # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 22  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 23  # DEALINGS IN THE SOFTWARE. 
 24   
 25  __all__ = ('DBusException', 'MissingErrorHandlerException', 
 26             'MissingReplyHandlerException', 'ValidationException', 
 27             'IntrospectionParserException', 'UnknownMethodException', 
 28             'NameExistsException') 
 29   
 30  from dbus._compat import is_py3 
 31   
 32   
33 -class DBusException(Exception):
34 35 include_traceback = False 36 """If True, tracebacks will be included in the exception message sent to 37 D-Bus clients. 38 39 Exceptions that are not DBusException subclasses always behave 40 as though this is True. Set this to True on DBusException subclasses 41 that represent a programming error, and leave it False on subclasses that 42 represent an expected failure condition (e.g. a network server not 43 responding).""" 44
45 - def __init__(self, *args, **kwargs):
46 name = kwargs.pop('name', None) 47 if name is not None or getattr(self, '_dbus_error_name', None) is None: 48 self._dbus_error_name = name 49 if kwargs: 50 raise TypeError('DBusException does not take keyword arguments: %s' 51 % ', '.join(kwargs.keys())) 52 Exception.__init__(self, *args)
53
54 - def __unicode__(self):
55 """Return a unicode error""" 56 # We can't just use Exception.__unicode__ because it chains up weirdly. 57 # https://code.launchpad.net/~mvo/ubuntu/quantal/dbus-python/lp846044/+merge/129214 58 if len(self.args) > 1: 59 s = unicode(self.args) 60 else: 61 s = ''.join(self.args) 62 63 if self._dbus_error_name is not None: 64 return '%s: %s' % (self._dbus_error_name, s) 65 else: 66 return s
67
68 - def __str__(self):
69 """Return a str error""" 70 s = Exception.__str__(self) 71 if self._dbus_error_name is not None: 72 return '%s: %s' % (self._dbus_error_name, s) 73 else: 74 return s
75
76 - def get_dbus_message(self):
77 if len(self.args) > 1: 78 if is_py3: 79 s = str(self.args) 80 else: 81 s = unicode(self.args) 82 else: 83 s = ''.join(self.args) 84 85 if isinstance(s, bytes): 86 return s.decode('utf-8', 'replace') 87 88 return s
89
90 - def get_dbus_name(self):
91 return self._dbus_error_name
92
93 -class MissingErrorHandlerException(DBusException):
94 95 include_traceback = True 96
97 - def __init__(self):
98 DBusException.__init__(self, "error_handler not defined: if you define a reply_handler you must also define an error_handler")
99
100 -class MissingReplyHandlerException(DBusException):
101 102 include_traceback = True 103
104 - def __init__(self):
105 DBusException.__init__(self, "reply_handler not defined: if you define an error_handler you must also define a reply_handler")
106
107 -class ValidationException(DBusException):
108 109 include_traceback = True 110
111 - def __init__(self, msg=''):
112 DBusException.__init__(self, "Error validating string: %s"%msg)
113
114 -class IntrospectionParserException(DBusException):
115 116 include_traceback = True 117
118 - def __init__(self, msg=''):
119 DBusException.__init__(self, "Error parsing introspect data: %s"%msg)
120
121 -class UnknownMethodException(DBusException):
122 123 include_traceback = True 124 _dbus_error_name = 'org.freedesktop.DBus.Error.UnknownMethod' 125
126 - def __init__(self, method):
127 DBusException.__init__(self, "Unknown method: %s"%method)
128
129 -class NameExistsException(DBusException):
130 131 include_traceback = True 132
133 - def __init__(self, name):
134 DBusException.__init__(self, "Bus name already exists: %s"%name)
135