# Slixmpp: The Slick XMPP Library# Copyright (C) 2010 Nathanael C. Fritz# This file is part of Slixmpp.# See the file LICENSE for copying permission.importloggingfromslixmpp.exceptionsimportXMPPError,IqError,IqTimeoutfromslixmpp.stanzaimportErrorfromslixmpp.xmlstreamimportET,StanzaBase,register_stanza_pluginlog=logging.getLogger(__name__)
[docs]classRootStanza(StanzaBase):""" 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 """
[docs]defexception(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 """ifisinstance(e,IqError):# We received an Iq error reply, but it wasn't caught# locally. Using the condition/text from that error# response could leak too much information, so we'll# only use a generic error here.reply=self.reply()reply['error']['condition']='undefined-condition'reply['error']['text']='External error'reply['error']['type']='cancel'log.warning('You should catch IqError exceptions',exc_info=True)reply.send()elifisinstance(e,IqTimeout):reply=self.reply()reply['error']['condition']='remote-server-timeout'reply['error']['type']='wait'log.warning('You should catch IqTimeout exceptions',exc_info=True)reply.send()elifisinstance(e,XMPPError):# We raised this deliberatelykeep_id=self['id']reply=self.reply(clear=e.clear)reply['id']=keep_idreply['error']['condition']=e.conditionreply['error']['text']=e.textreply['error']['type']=e.etypeife.by:reply["error"]["by"]=e.byife.extensionisnotNone:# Extended error tagextxml=ET.Element("{%s}%s"%(e.extension_ns,e.extension),e.extension_args)reply['error'].append(extxml)reply.send()else:# We probably didn't raise this on purpose, so send an error stanzakeep_id=self['id']reply=self.reply()reply['id']=keep_idreply['error']['condition']='undefined-condition'reply['error']['text']="Slixmpp got into trouble."reply['error']['type']='cancel'reply.send()# log the errorlog.exception('Error handling {%s}%s stanza',self.namespace,self.name)# Finally raise the exception to a global exception handlerself.stream.exception(e)