File: errors.py

package info (click to toggle)
python-nbxmpp 6.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,340 kB
  • sloc: python: 19,639; makefile: 4
file content (169 lines) | stat: -rw-r--r-- 4,705 bytes parent folder | download | duplicates (2)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Copyright (C) 2020 Philipp Hörist <philipp AT hoerist.com>
#
# This file is part of nbxmpp.
#
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import annotations

from typing import Any

import logging

from nbxmpp.modules.dataforms import MultipleDataForm
from nbxmpp.modules.dataforms import SimpleDataForm
from nbxmpp.namespaces import Namespace
from nbxmpp.protocol import Protocol
from nbxmpp.structs import RegisterData


def is_error(error: Any) -> bool:
    return isinstance(error, BaseError)


class BaseError(Exception):
    def __init__(self, is_fatal: bool = False) -> None:
        self.is_fatal = is_fatal
        self.text = ""

    def __str__(self) -> str:
        return self.text

    def get_text(self, _pref_lang: str | None = None) -> str:
        return self.text


class StanzaError(BaseError):

    log_level = logging.INFO
    app_namespace = None

    def __init__(self, stanza: Protocol) -> None:
        BaseError.__init__(self)
        self.stanza = stanza
        self._stanza_name = stanza.getName()
        self._error_node = stanza.getTag("error")
        self.condition: str | None = stanza.getError()
        self.condition_data = self._error_node.getTagData(self.condition)
        self.app_condition = self._get_app_condition()
        self.type = stanza.getErrorType()
        self.jid = stanza.getFrom()
        self.id = stanza.getID()
        self._text: dict[str, str] = {}

        text_elements = self._error_node.getTags("text", namespace=Namespace.STANZAS)
        for element in text_elements:
            lang = element.getXmlLang()
            text = element.getData()
            self._text[lang] = text

    def _get_app_condition(self) -> str | None:
        if self.app_namespace is None:
            return None

        for node in self._error_node.getChildren():
            if node.getNamespace() == self.app_namespace:
                return node.getName()
        return None

    def get_text(self, pref_lang: str | None = None) -> str:
        if pref_lang is not None:
            text = self._text.get(pref_lang)
            if text is not None:
                return text

        if self._text:
            text = self._text.get("en")
            if text is not None:
                return text

            text = self._text.get(None)
            if text is not None:
                return text
            return self._text.popitem()[1]
        return ""

    def set_text(self, lang: str, text: str) -> None:
        self._text[lang] = text

    def __str__(self) -> str:
        condition = self.condition
        if self.app_condition is not None:
            condition = "%s (%s)" % (self.condition, self.app_condition)
        text = self.get_text("en") or ""
        if text:
            text = " - %s" % text
        return "Error from %s: %s%s" % (self.jid, condition, text)


class PubSubStanzaError(StanzaError):

    app_namespace = Namespace.PUBSUB_ERROR


class HTTPUploadStanzaError(StanzaError):

    app_namespace = Namespace.HTTPUPLOAD_0

    def get_max_file_size(self) -> float | None:
        if self.app_condition != "file-too-large":
            return None

        node = self._error_node.getTag(self.app_condition)
        try:
            return float(node.getTagData("max-file-size"))
        except Exception:
            return None

    def get_retry_date(self) -> str | None:
        if self.app_condition != "retry":
            return None
        return self._error_node.getTagAttr("stamp")


class MalformedStanzaError(BaseError):

    log_level = logging.WARNING

    def __init__(self, text: str, stanza: Protocol, is_fatal: bool = True) -> None:
        BaseError.__init__(self, is_fatal=is_fatal)
        self.stanza = stanza
        self.text = str(text)


class CancelledError(BaseError):

    log_level = logging.INFO

    def __init__(self) -> None:
        BaseError.__init__(self, is_fatal=True)
        self.text = "Task has been cancelled"


class TimeoutStanzaError(BaseError):

    log_level = logging.INFO

    def __init__(self) -> None:
        BaseError.__init__(self)
        self.text = "Timeout reached"


class RegisterStanzaError(StanzaError):
    def __init__(self, stanza: Protocol, data: RegisterData) -> None:
        StanzaError.__init__(self, stanza)
        self._data = data

    def get_data(self) -> RegisterData:
        return self._data


class ChangePasswordStanzaError(StanzaError):
    def __init__(
        self, stanza: Protocol, form: SimpleDataForm | MultipleDataForm
    ) -> None:
        StanzaError.__init__(self, stanza)
        self._form = form

    def get_form(self) -> SimpleDataForm | MultipleDataForm:
        return self._form