summaryrefslogtreecommitdiff
path: root/sleekxmpp/stanza/rootstanza.py
blob: eafc79a2c9e824252b3e21b6bc14a4a704837f2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
    SleekXMPP: The Sleek XMPP Library
    Copyright (C) 2010  Nathanael C. Fritz
    This file is part of SleekXMPP.

    See the file LICENSE for copying permission.
"""

import logging
import traceback
import sys

from sleekxmpp.exceptions import XMPPError
from sleekxmpp.stanza import Error
from sleekxmpp.xmlstream.stanzabase import ET, StanzaBase, registerStanzaPlugin


class RootStanza(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
    """

    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()
        if isinstance(e, XMPPError):
            # We raised this deliberately
            self['error']['condition'] = e.condition
            self['error']['text'] = e.text
            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
        else:
            # We probably didn't raise this on purpose, so send a traceback
            self['error']['condition'] = 'undefined-condition'
            if sys.version_info < (3, 0):
                self['error']['text'] = "SleekXMPP got into trouble."
            else:
                self['error']['text'] = traceback.format_tb(e.__traceback__)
                logging.exception('Error handling {%s}%s stanza' %
                                  (self.namespace, self.name))
        self.send()


registerStanzaPlugin(RootStanza, Error)