summaryrefslogtreecommitdiff
path: root/sleekxmpp/stanza/rootstanza.py
diff options
context:
space:
mode:
authorLance Stout <lancestout@gmail.com>2011-08-19 00:08:47 -0700
committerLance Stout <lancestout@gmail.com>2011-08-19 01:04:20 -0700
commitf92f96325ac12160554cdf94d98b6742671d67a1 (patch)
tree9b6057be903a1f96324f1c252f1141a6c130c8f8 /sleekxmpp/stanza/rootstanza.py
parentb98555c512ddb7452c9a217b1024b9e3163dea99 (diff)
downloadslixmpp-f92f96325ac12160554cdf94d98b6742671d67a1.tar.gz
slixmpp-f92f96325ac12160554cdf94d98b6742671d67a1.tar.bz2
slixmpp-f92f96325ac12160554cdf94d98b6742671d67a1.tar.xz
slixmpp-f92f96325ac12160554cdf94d98b6742671d67a1.zip
Make Iq exceptions more discoverable and simpler to use.
IqError and IqTimeout now extend XMPPError, so if you don't care about the difference, you can use: try: self.do_something_with_iqs() except XMPPError: # Error? Timeout? I don't care! pass If you do need to distinguish between timeouts and error replies, you can still continue to use: try: self.do_somethin_with_iqs() except IqError as err: pass except IqTimeout: pass If you don't catch any Iq errors and you're processing a stanza then an error response will be sent, just like normal if you raise XMPPError or any other exception, except that the error messages will be generic to prevent leaking too much information.
Diffstat (limited to 'sleekxmpp/stanza/rootstanza.py')
-rw-r--r--sleekxmpp/stanza/rootstanza.py28
1 files changed, 23 insertions, 5 deletions
diff --git a/sleekxmpp/stanza/rootstanza.py b/sleekxmpp/stanza/rootstanza.py
index 9e1d1cfa..470a1225 100644
--- a/sleekxmpp/stanza/rootstanza.py
+++ b/sleekxmpp/stanza/rootstanza.py
@@ -10,7 +10,7 @@ import logging
import traceback
import sys
-from sleekxmpp.exceptions import XMPPError
+from sleekxmpp.exceptions import XMPPError, IqError, IqTimeout
from sleekxmpp.stanza import Error
from sleekxmpp.xmlstream import ET, StanzaBase, register_stanza_plugin
@@ -43,23 +43,41 @@ class RootStanza(StanzaBase):
Arguments:
e -- Exception object
"""
- if isinstance(e, XMPPError):
- self.reply(clear=e.clear)
+ if isinstance(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.
+ self.reply()
+ self['error']['condition'] = 'undefined-condition'
+ self['error']['text'] = 'External error'
+ self['error']['type'] = 'cancel'
+ log.warning('You should catch IqError exceptions')
+ self.send()
+ elif isinstance(e, IqTimeout):
+ self.reply()
+ self['error']['condition'] = 'remote-server-timeout'
+ self['error']['type'] = 'wait'
+ log.warning('You should catch IqTimeout exceptions')
+ self.send()
+ elif isinstance(e, XMPPError):
# We raised this deliberately
+ self.reply(clear=e.clear)
self['error']['condition'] = e.condition
self['error']['text'] = e.text
+ self['error']['type'] = e.etype
if e.extension is not None:
# Extended error tag
extxml = ET.Element("{%s}%s" % (e.extension_ns, e.extension),
e.extension_args)
self['error'].append(extxml)
- self['error']['type'] = e.etype
self.send()
else:
- self.reply()
# We probably didn't raise this on purpose, so send an error stanza
+ self.reply()
self['error']['condition'] = 'undefined-condition'
self['error']['text'] = "SleekXMPP got into trouble."
+ self['error']['type'] = 'cancel'
self.send()
# log the error
log.exception('Error handling {%s}%s stanza' %