~sirn/fanboi2

ref: baf63a21556905a6c27516a8d5641d31120d1fe3 fanboi2/fanboi2/errors.py -rw-r--r-- 5.5 KiB
baf63a21Kridsada Thanabulpong Add scope column to banwords. 1 year, 8 months ago
                                                                                
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
class BaseError(Exception):
    """A base :class:`Exception` class that provides hint for reporting
    errors as JSON response.
    """

    def message(self, request):
        """A serializable object to be include in the response as error
        message. The return type of this method is not necessary a string,
        it may be a list of string, a dict containing string or some other
        serializable types.

        :param request: A :class:`pyramid.request.Request` object.
        """
        return "An exception error occurred."

    @property
    def name(self):
        """The short globally recognizable name of this error."""
        return "unknown"

    @property
    def http_status(self):
        """The HTTP status code to response as."""
        return "500 Internal Server Error"


class RateLimitedError(BaseError):
    """An :class:`Exception` class that will be raised if user request was
    blocked due to rate limiter. The time left until user is unblocked could
    be accessed from :property:`timeleft`.

    :param timeleft: An :type:`int` in seconds until user is unblocked.
    """

    def __init__(self, timeleft):
        self.timeleft = timeleft

    def message(self, request):
        return "Please wait %s seconds before retrying." % self.timeleft

    @property
    def name(self):
        return "rate_limited"

    @property
    def http_status(self):
        return "429 Too Many Requests"


class ParamsInvalidError(BaseError):
    """An :class:`Exception` class that will be raised if user request could
    not be processed due to form being invalid. Error messages are stored
    inside :property:`messages`.

    :param messages: An :type:`dict` of :type:`list` of field errors.
    """

    def __init__(self, messages):
        self.messages = messages

    def message(self, request):
        return self.messages

    @property
    def name(self):
        return "params_invalid"

    @property
    def http_status(self):
        return "400 Bad Request"


class AkismetRejectedError(BaseError):
    """An :class:`Exception` class that will be raised if user request was
    blocked due user request failed a spam check.
    """

    def message(self, request):
        return (
            "The request has been identified as spam "
            + "by Akismet and therefore rejected."
        )

    @property
    def name(self):
        return "akismet_rejected"

    @property
    def http_status(self):
        return "422 Unprocessable Entity"


class DNSBLRejectedError(BaseError):
    """An :class:`Exception` class that will be raised if user request was
    blocked due user IP address failed an DNSBL check.
    """

    def message(self, request):
        return (
            "The IP address is being listed in one of DNSBL databases "
            + "and therefore rejected."
        )

    @property
    def name(self):
        return "dnsbl_rejected"

    @property
    def http_status(self):
        return "422 Unprocessable Entity"


class BanRejectedError(BaseError):
    """An :class:`Exception` class that will be raised if user request was
    blocked due user IP address being listed in the ban list.
    """

    def message(self, request):
        return (
            "The IP address is being listed in the ban list "
            + "and therefore rejected."
        )

    @property
    def name(self):
        return "ban_rejected"

    @property
    def http_status(self):
        return "422 Unprocessable Entity"


class BanwordRejectedError(BaseError):
    """An :class:`Exception` class that will be raised if user request was
    blocked due user input text being listed in the banword.
    """

    def message(self, request):
        return "The message contained a forbidden keyword and therefore rejected."

    @property
    def name(self):
        return "banword_rejected"

    @property
    def http_status(self):
        return "422 Unprocessable Entity"


class StatusRejectedError(BaseError):
    """An :class:`Exception` class that will be raised if user request was
    blocked due the topic or the board being locked. The status that caused
    the lock could be retrieved from :property:`status`.

    :param status: A :type:`str` of the status that caused the block.
    """

    def __init__(self, status):
        self.status = status

    def message(self, request):
        return "The topic or the board is currently locked."

    @property
    def name(self):
        return "status_rejected"

    @property
    def http_status(self):
        return "422 Unprocessable Entity"


class ProxyRejectedError(BaseError):
    """An :class:`Exception` class that will be raised if user request was
    identified to be an open proxy or public VPN service.
    """

    def message(self, request):
        return (
            "The IP address has been identified as an open proxy "
            + "or VPN service and therefore rejected."
        )

    @property
    def name(self):
        return "proxy_rejected"

    @property
    def http_status(self):
        return "422 Unprocessable Entity"


_ERRORS = {
    "rate_limited": RateLimitedError,
    "params_invalid": ParamsInvalidError,
    "akismet_rejected": AkismetRejectedError,
    "dnsbl_rejected": DNSBLRejectedError,
    "ban_rejected": BanRejectedError,
    "banword_rejected": BanwordRejectedError,
    "status_rejected": StatusRejectedError,
    "proxy_rejected": ProxyRejectedError,
}


def deserialize_error(type_):
    """Deserialize the given error type string into an error class."""
    return _ERRORS.get(type_, BaseError)