2010-03-26 21:32:16 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
2010-07-20 15:19:49 +00:00
|
|
|
See the file LICENSE for copying permission.
|
2010-03-26 21:32:16 +00:00
|
|
|
"""
|
2010-07-28 17:14:41 +00:00
|
|
|
|
2010-07-27 01:02:25 +00:00
|
|
|
import logging
|
2010-01-05 21:56:48 +00:00
|
|
|
import traceback
|
2010-04-23 04:24:28 +00:00
|
|
|
import sys
|
2010-01-05 21:56:48 +00:00
|
|
|
|
2010-07-28 17:14:41 +00:00
|
|
|
from sleekxmpp.exceptions import XMPPError
|
|
|
|
from sleekxmpp.stanza import Error
|
2010-10-18 01:38:22 +00:00
|
|
|
from sleekxmpp.xmlstream import ET, StanzaBase, register_stanza_plugin
|
2010-07-28 17:14:41 +00:00
|
|
|
|
|
|
|
|
2010-11-06 05:28:59 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2010-01-05 21:56:48 +00:00
|
|
|
class RootStanza(StanzaBase):
|
|
|
|
|
2010-07-28 17:14:41 +00:00
|
|
|
"""
|
|
|
|
A top-level XMPP stanza in an XMLStream.
|
|
|
|
|
|
|
|
The RootStanza class provides a more XMPP specific exception
|
|
|
|
handler than provided by the generic StanzaBase class.
|
|
|
|
|
|
|
|
Methods:
|
|
|
|
exception -- Overrides StanzaBase.exception
|
|
|
|
"""
|
|
|
|
|
|
|
|
def exception(self, e):
|
|
|
|
"""
|
|
|
|
Create and send an error reply.
|
|
|
|
|
|
|
|
Typically called when an event handler raises an exception.
|
|
|
|
The error's type and text content are based on the exception
|
|
|
|
object's type and content.
|
|
|
|
|
|
|
|
Overrides StanzaBase.exception.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
e -- Exception object
|
|
|
|
"""
|
|
|
|
self.reply()
|
2010-07-29 15:02:42 +00:00
|
|
|
if isinstance(e, XMPPError):
|
2010-07-28 17:14:41 +00:00
|
|
|
# We raised this deliberately
|
|
|
|
self['error']['condition'] = e.condition
|
|
|
|
self['error']['text'] = e.text
|
2010-07-29 15:02:42 +00:00
|
|
|
if e.extension is not None:
|
2010-07-28 17:14:41 +00:00
|
|
|
# Extended error tag
|
2010-07-29 15:02:42 +00:00
|
|
|
extxml = ET.Element("{%s}%s" % (e.extension_ns, e.extension),
|
|
|
|
e.extension_args)
|
2010-07-28 17:14:41 +00:00
|
|
|
self['error'].append(extxml)
|
|
|
|
self['error']['type'] = e.etype
|
2010-07-29 15:02:42 +00:00
|
|
|
else:
|
|
|
|
# We probably didn't raise this on purpose, so send a traceback
|
2010-07-28 17:14:41 +00:00
|
|
|
self['error']['condition'] = 'undefined-condition'
|
2010-07-29 15:02:42 +00:00
|
|
|
if sys.version_info < (3, 0):
|
2010-07-28 17:14:41 +00:00
|
|
|
self['error']['text'] = "SleekXMPP got into trouble."
|
|
|
|
else:
|
|
|
|
self['error']['text'] = traceback.format_tb(e.__traceback__)
|
2010-11-06 05:28:59 +00:00
|
|
|
log.exception('Error handling {%s}%s stanza' %
|
2010-07-29 15:02:42 +00:00
|
|
|
(self.namespace, self.name))
|
2010-07-28 17:14:41 +00:00
|
|
|
self.send()
|
|
|
|
|
|
|
|
|
2010-10-18 01:38:22 +00:00
|
|
|
register_stanza_plugin(RootStanza, Error)
|