1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
# MIT License |
3
|
|
|
# |
4
|
|
|
# Copyright (c) 2021 Pincer |
5
|
|
|
# |
6
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining |
7
|
|
|
# a copy of this software and associated documentation files |
8
|
|
|
# (the "Software"), to deal in the Software without restriction, |
9
|
|
|
# including without limitation the rights to use, copy, modify, merge, |
10
|
|
|
# publish, distribute, sublicense, and/or sell copies of the Software, |
11
|
|
|
# and to permit persons to whom the Software is furnished to do so, |
12
|
|
|
# subject to the following conditions: |
13
|
|
|
# |
14
|
|
|
# The above copyright notice and this permission notice shall be |
15
|
|
|
# included in all copies or substantial portions of the Software. |
16
|
|
|
# |
17
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
18
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
19
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
20
|
|
|
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
21
|
|
|
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
22
|
|
|
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
23
|
|
|
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
24
|
|
|
|
25
|
|
|
from typing import Optional |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class PincerError(Exception): |
29
|
|
|
"""Base exception class for all Pincer errors.""" |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class UnhandledException(PincerError): |
33
|
|
|
""" |
34
|
|
|
Exception which gets thrown if an exception wasn't handled. |
35
|
|
|
|
36
|
|
|
Please create an issue on our github |
37
|
|
|
if this exception gets thrown. |
38
|
|
|
""" |
39
|
|
|
|
40
|
|
|
def __init__(self, specific: str): |
41
|
|
|
super(UnhandledException, self).__init__( |
42
|
|
|
specific + " Please report this to the library devs." |
43
|
|
|
) |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
class CommandError(PincerError): |
47
|
|
|
""" |
48
|
|
|
Base class for exceptions which are related to commands. |
49
|
|
|
""" |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
class CommandIsNotCoroutine(CommandError): |
53
|
|
|
""" |
54
|
|
|
Exception raised when the provided command call is not a coroutine. |
55
|
|
|
""" |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
class CommandAlreadyRegistered(CommandError): |
59
|
|
|
""" |
60
|
|
|
The command which you are trying to register is already registered. |
61
|
|
|
""" |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
class CommandDescriptionTooLong(CommandError): |
65
|
|
|
""" |
66
|
|
|
The provided command description is too long, as it exceeds 100 characters. |
67
|
|
|
""" |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
class TooManyArguments(CommandError): |
71
|
|
|
""" |
72
|
|
|
A command can have a maximum of 25 arguments. |
73
|
|
|
If this number of arguments gets exceeded, this exception will be raised. |
74
|
|
|
""" |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
class InvalidArgumentAnnotation(CommandError): |
78
|
|
|
""" |
79
|
|
|
The provided argument annotation is not known, so it cannot be used. |
80
|
|
|
""" |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
class InvalidEventName(PincerError): |
84
|
|
|
""" |
85
|
|
|
Exception raised when the event name is not a valid event. |
86
|
|
|
This can be because the event name did not begin with an ``on_`` or |
87
|
|
|
because its not a valid event in the library. |
88
|
|
|
""" |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
class InvalidUrlError(PincerError, ValueError): |
92
|
|
|
""" |
93
|
|
|
Exception raised when an invalid url has been provided. |
94
|
|
|
""" |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
class EmbedFieldError(PincerError, ValueError): |
98
|
|
|
"""Exception that is raised when an embed field is too large""" |
99
|
|
|
|
100
|
|
|
@classmethod |
101
|
|
|
def from_desc(cls, _type: str, max_size: int, cur_size: int): |
102
|
|
|
""" |
103
|
|
|
Create an instance by description. |
104
|
|
|
|
105
|
|
|
:param _type: |
106
|
|
|
The type/name of the field. |
107
|
|
|
|
108
|
|
|
:param max_size: |
109
|
|
|
The maximum size of the field. |
110
|
|
|
|
111
|
|
|
:param cur_size: |
112
|
|
|
The current size of the field. |
113
|
|
|
""" |
114
|
|
|
return cls( |
115
|
|
|
f"{_type} can have a maximum length of {max_size}." |
116
|
|
|
f" (Current size: {cur_size})" |
117
|
|
|
) |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
class DispatchError(PincerError): |
121
|
|
|
""" |
122
|
|
|
Base exception class for all errors which are specifically related |
123
|
|
|
to the dispatcher. |
124
|
|
|
""" |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
class _InternalPerformReconnectError(DispatchError): |
128
|
|
|
"""Internal helper exception which on raise lets the client reconnect.""" |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
class DisallowedIntentsError(DispatchError): |
132
|
|
|
""" |
133
|
|
|
Invalid gateway intent got provided. |
134
|
|
|
Make sure your client has the enabled intent. |
135
|
|
|
""" |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
class InvalidTokenError(DispatchError, ValueError): |
139
|
|
|
""" |
140
|
|
|
Exception raised when the authorization token is invalid. |
141
|
|
|
""" |
142
|
|
|
|
143
|
|
|
def __init__(self, hint: Optional[str] = None): |
144
|
|
|
""" |
145
|
|
|
:param hint: |
146
|
|
|
Additional information about the exception cause. |
147
|
|
|
""" |
148
|
|
|
hint = hint or '' |
149
|
|
|
|
150
|
|
|
super(InvalidTokenError, self).__init__( |
151
|
|
|
"The given token is not a valid token.\n" + hint |
152
|
|
|
) |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
class HeartbeatError(DispatchError): |
156
|
|
|
"""Exception raised due to a problem with websocket heartbeat.""" |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
class UnavailableGuildError(PincerError): |
160
|
|
|
""" |
161
|
|
|
Exception raised due to a guild being unavailable. |
162
|
|
|
This is caused by a discord outage. |
163
|
|
|
""" |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
# Discord HTTP Errors |
167
|
|
|
# `developers/docs/topics/opcodes-and-status-codes#http` |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
class HTTPError(PincerError): |
171
|
|
|
"""HTTP Exception base class.""" |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
class NotModifiedError(HTTPError): |
175
|
|
|
"""Error code 304.""" |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
class BadRequestError(HTTPError): |
179
|
|
|
"""Error code 400.""" |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
class UnauthorizedError(HTTPError): |
183
|
|
|
"""Error code 401.""" |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
class ForbiddenError(HTTPError): |
187
|
|
|
"""Error code 403.""" |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
class NotFoundError(HTTPError): |
191
|
|
|
"""Error code 404.""" |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
class MethodNotAllowedError(HTTPError): |
195
|
|
|
"""Error code 405.""" |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
class RateLimitError(HTTPError): |
199
|
|
|
"""Error code 429.""" |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
class GatewayError(HTTPError): |
203
|
|
|
"""Error code 502.""" |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
class ServerError(HTTPError): |
207
|
|
|
""" |
208
|
|
|
Error code 5xx. |
209
|
|
|
Status code is not in the discord API |
210
|
|
|
""" |
211
|
|
|
|