exceptions.py 4.6 KB

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