exceptions.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. """
  3. sleekxmpp.exceptions
  4. ~~~~~~~~~~~~~~~~~~~~
  5. Part of SleekXMPP: The Sleek XMPP Library
  6. :copyright: (c) 2011 Nathanael C. Fritz
  7. :license: MIT, see LICENSE for more details
  8. """
  9. class XMPPError(Exception):
  10. """
  11. A generic exception that may be raised while processing an XMPP stanza
  12. to indicate that an error response stanza should be sent.
  13. The exception method for stanza objects extending
  14. :class:`~sleekxmpp.stanza.rootstanza.RootStanza` will create an error
  15. stanza and initialize any additional substanzas using the extension
  16. information included in the exception.
  17. Meant for use in SleekXMPP plugins and applications using SleekXMPP.
  18. Extension information can be included to add additional XML elements
  19. to the generated error stanza.
  20. :param condition: The XMPP defined error condition.
  21. Defaults to ``'undefined-condition'``.
  22. :param text: Human readable text describing the error.
  23. :param etype: The XMPP error type, such as ``'cancel'`` or ``'modify'``.
  24. Defaults to ``'cancel'``.
  25. :param extension: Tag name of the extension's XML content.
  26. :param extension_ns: XML namespace of the extensions' XML content.
  27. :param extension_args: Content and attributes for the extension
  28. element. Same as the additional arguments to
  29. the :class:`~xml.etree.ElementTree.Element`
  30. constructor.
  31. :param clear: Indicates if the stanza's contents should be
  32. removed before replying with an error.
  33. Defaults to ``True``.
  34. """
  35. def __init__(self, condition='undefined-condition', text='',
  36. etype='cancel', extension=None, extension_ns=None,
  37. extension_args=None, clear=True):
  38. if extension_args is None:
  39. extension_args = {}
  40. self.condition = condition
  41. self.text = text
  42. self.etype = etype
  43. self.clear = clear
  44. self.extension = extension
  45. self.extension_ns = extension_ns
  46. self.extension_args = extension_args
  47. class IqTimeout(XMPPError):
  48. """
  49. An exception which indicates that an IQ request response has not been
  50. received within the alloted time window.
  51. """
  52. def __init__(self, iq):
  53. super(IqTimeout, self).__init__(
  54. condition='remote-server-timeout',
  55. etype='cancel')
  56. #: The :class:`~sleekxmpp.stanza.iq.Iq` stanza whose response
  57. #: did not arrive before the timeout expired.
  58. self.iq = iq
  59. class IqError(XMPPError):
  60. """
  61. An exception raised when an Iq stanza of type 'error' is received
  62. after making a blocking send call.
  63. """
  64. def __init__(self, iq):
  65. super(IqError, self).__init__(
  66. condition=iq['error']['condition'],
  67. text=iq['error']['text'],
  68. etype=iq['error']['type'])
  69. #: The :class:`~sleekxmpp.stanza.iq.Iq` error result stanza.
  70. self.iq = iq