pincer.exceptions.EmbedFieldError.from_desc()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 15
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 1
nop 4
1
# Copyright Pincer 2021-Present
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# Full MIT License can be found in `LICENSE` at the project root.
3
from typing import Optional
4
5
6
class PincerError(Exception):
7
    """Base exception class for all Pincer errors"""
8
9
10
class InvalidPayload(PincerError):
11
    """Exception which gets thrown if an invalid payload has been received.
12
    This means that the data of the payload did not match the expected
13
    format and/or didn't contain the expected values.
14
    """
15
16
17
class UnhandledException(PincerError):
18
    """Exception which gets thrown if an exception wasn't handled.
19
20
    Please create an issue on our GitHub
21
    if this exception gets thrown.
22
    """
23
24
    def __init__(self, specific: str):
25
        super(UnhandledException, self).__init__(
26
            specific + " Please report this to the library devs."
27
        )
28
29
30
class NoExportMethod(PincerError):
31
    """Exception which gets raised when an `export` method is expected but
32
    not found in a module.
33
    """
34
35
36
class CogError(PincerError):
37
    """Exception base class for errors related to Cogs."""
38
39
40
class CogNotFound(CogError):
41
    """Exception which gets raised when a cog is trying to be
42
    loaded/unloaded but is nonexistent.
43
    """
44
45
46
class CogAlreadyExists(CogError):
47
    """Exception which gets raised when a cog is already loaded, but is
48
    trying to be reloaded!
49
    """
50
51
52
class NoValidSetupMethod(CogError):
53
    """Exception which gets raised when an `setup` function is expected but
54
    none was found!
55
    """
56
57
58
class TooManySetupArguments(CogError):
59
    """Exception which gets raised when too many arguments were requested
60
    in a cog its setup function.
61
    """
62
63
64
class NoCogManagerReturnFound(CogError):
65
    """Exception which gets raised when no cog return was found from the
66
    setup function. (are you missing a return statement?)
67
    """
68
69
70
class CommandError(PincerError):
71
    """Base class for exceptions which are related to command."""
72
73
74
class CommandCooldownError(CommandError):
75
    """Exception which gets raised when a command cooldown has not been
76
    breached.
77
78
    Attributes
79
    ----------
80
    ctx: :class:`~objects.message.context.MessageContext`
81
        The context of the error
82
    """
83
84
    def __init__(self, message: str, context):
85
        self.ctx = context
86
        super(CommandCooldownError, self).__init__(message)
87
88
89
class CommandIsNotCoroutine(CommandError):
90
    """Exception raised when the provided command call is not a coroutine."""
91
92
93
class CommandAlreadyRegistered(CommandError):
94
    """The command which you are trying to register is already registered."""
95
96
97
class CommandDescriptionTooLong(CommandError):
98
    """The provided command description is too long,
99
    as it exceeds 100 characters.
100
    """
101
102
103
class TooManyArguments(CommandError):
104
    """A command can have a maximum of 25 arguments.
105
    If this number of arguments gets exceeded, this exception will be raised.
106
    """
107
108
109
class InvalidArgumentAnnotation(CommandError):
110
    """The provided argument annotation is not known, so it cannot be used."""
111
112
113
class CommandReturnIsEmpty(CommandError):
114
    """Cannot return an empty string to an interaction."""
115
116
117
class InvalidCommandGuild(CommandError):
118
    """The provided guild id not not valid."""
119
120
121
class InteractionDoesNotExist(CommandError):
122
    """The action which you are trying to perform requires an
123
    interaction to be created/sent to discord. But this has not been
124
    done yet!
125
    """
126
127
128
class UseFollowup(CommandError):
129
    """A reply has already been sent, please use a followup to
130
    continue replying.
131
    """
132
133
134
class InteractionAlreadyAcknowledged(CommandError):
135
    """The command has already been acknowledged by discord.
136
    This can be because a reply or ack has already been sent!
137
    """
138
139
140
class InteractionTimedOut(CommandError):
141
    """Discord had to wait too long for a response from your command!
142
    The discord wait time can be extended by using the
143
    :func:`~pincer.objects.app.interaction.Interaction.ack` function in
144
    the :attr:`~pincer.objects.message.context.MessageContext.interaction`
145
    property.
146
    """
147
148
149
class InvalidCommandName(CommandError):
150
    """Exception raised when the command is considered invalid.
151
    This is caused by a name that doesn't match the command name regex.
152
    """
153
154
155
class InvalidEventName(PincerError):
156
    """Exception raised when the event name is not a valid event.
157
    This can be because the event name did not begin with an ``on_`` or
158
    because it's not a valid event in the library.
159
    """
160
161
162
class InvalidUrlError(PincerError, ValueError):
163
    """Exception raised when an invalid url has been provided."""
164
165
166
class ImageEncodingError(PincerError):
167
    """Exception raised when an image cannot be encoded for Discord"""
168
169
170
class EmbedFieldError(PincerError, ValueError):
171
    """Exception that is raised when an embed field is too large."""
172
173
    @classmethod
174
    def from_desc(cls, _type: str, max_size: int, cur_size: int):
175
        """Create an instance by description.
176
177
        Parameters
178
        ----------
179
        _type :class:`str`
180
            The type/name of the field.
181
        max_si :class:`int`
182
            The maximum size of the field.
183
        cur_size :class:`int`
184
            The current size of the field.
185
        """
186
        return cls(
187
            f"{_type} can have a maximum length of {max_size}."
188
            f" (Current size: {cur_size})"
189
        )
190
191
192
class EmbedOverflow(PincerError):
193
    """Exception that is raised when too many embeds are passed in."""
194
195
196
class TaskError(PincerError):
197
    """Base class for exceptions that are related to task.
198
199
    Attributes
200
    ----------
201
    task: :class:`~utils.tasks.Task`
202
        The task that raised the exception.
203
    """
204
205
    def __init__(self, message: str, task=None):
206
        self.task = task
207
        super().__init__(message)
208
209
210
class TaskAlreadyRunning(TaskError):
211
    """Exception that is raised when the user tries to start a running task."""
212
213
214
class TaskCancelError(TaskError):
215
    """Exception that is raised when a task cannot be cancelled."""
216
217
218
class TaskIsNotCoroutine(TaskError):
219
    """Exception that is raised when the provided function for a task is not
220
    a coroutine.
221
    """
222
223
224
class TaskInvalidDelay(TaskError):
225
    """Exception that is raised when the provided delay is invalid."""
226
227
228
class DispatchError(PincerError):
229
    """Base exception class for all errors which are specifically related
230
    to the dispatcher.
231
    """
232
233
234
class _InternalPerformReconnectError(DispatchError):
235
    """Internal helper exception which on raise lets the client reconnect."""
236
237
238
class DisallowedIntentsError(DispatchError):
239
    """Invalid gateway intent got provided.
240
    Make sure your client has the enabled intent.
241
    """
242
243
244
class InvalidTokenError(DispatchError, ValueError):
245
    """Exception raised when the authorization token is invalid."""
246
247
    def __init__(self, hint: Optional[str] = None):
248
        hint = hint or ""
249
250
        super(InvalidTokenError, self).__init__(
251
            "The given token is not a valid token.\n" + hint
252
        )
253
254
255
class HeartbeatError(DispatchError):
256
    """Exception raised due to a problem with websocket heartbeat."""
257
258
259
class UnavailableGuildError(PincerError):
260
    """Exception raised due to a guild being unavailable.
261
    This is caused by a discord outage.
262
    """
263
264
265
class TimeoutError(PincerError):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in TimeoutError.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
266
    """Exception raised when :class:`~pincer.utils.event_mgr.EventMgr`
267
    `wait_for` and `loop_for` methods time out
268
    """
269
270
271
class GatewayConnectionError(PincerError):
272
    """Could not connect to Discord gateway"""
273
274
275
# Discord HTTP Errors
276
# `developers/docs/topics/opcodes-and-status-codes#http`
277
278
279
class HTTPError(PincerError):
280
    """HTTP Exception base class."""
281
282
283
class NotModifiedError(HTTPError):
284
    """Error code 304."""
285
286
287
class BadRequestError(HTTPError):
288
    """Error code 400."""
289
290
291
class UnauthorizedError(HTTPError):
292
    """Error code 401."""
293
294
295
class ForbiddenError(HTTPError):
296
    """Error code 403."""
297
298
299
class NotFoundError(HTTPError):
300
    """Error code 404."""
301
302
303
class MethodNotAllowedError(HTTPError):
304
    """Error code 405."""
305
306
307
class RateLimitError(HTTPError):
308
    """Error code 429."""
309
310
311
class GatewayError(HTTPError):
312
    """Error code 502."""
313
314
315
class ServerError(HTTPError):
316
    """Error code 5xx."""
317