|
8 | 8 | from asks.response_objects import Response |
9 | 9 |
|
10 | 10 | __all__ = ( |
11 | | - 'JSONErrorCode', |
12 | 11 | 'ClamorError', |
13 | | - 'RequestFailed', |
14 | | - 'Unauthorized', |
| 12 | + 'EncodingFailed', |
15 | 13 | 'Forbidden', |
16 | | - 'NotFound', |
| 14 | + 'GatewayCloseCode', |
| 15 | + 'GatewayError', |
17 | 16 | 'Hierarchied', |
| 17 | + 'InvalidListener', |
| 18 | + 'JSONErrorCode', |
| 19 | + 'NotFound', |
| 20 | + 'RequestFailed', |
| 21 | + 'Unauthorized', |
18 | 22 | ) |
19 | 23 |
|
20 | 24 | logger = logging.getLogger(__name__) |
@@ -147,6 +151,48 @@ def name(self) -> str: |
147 | 151 | return ' '.join(part.capitalize() for part in self._name_.split('_')) |
148 | 152 |
|
149 | 153 |
|
| 154 | +class GatewayCloseCode(IntEnum): |
| 155 | + """Enum that holds the possible WebSocket close codes for gateway.""" |
| 156 | + |
| 157 | + #: Connection was closed gracefully (or heartbeats timed out). |
| 158 | + OK = 1000 |
| 159 | + #: Connection was closed due to CloudFlare load balancing. |
| 160 | + LOAD_BALANCING_CLOSURE = 1001 |
| 161 | + #: Random server error. |
| 162 | + RANDOM_SERVER_ERROR = 1006 |
| 163 | + |
| 164 | + #: Unknown error. |
| 165 | + UNKNOWN_ERROR = 4000 |
| 166 | + #: An invalid opcode or an invalid payload for an opcode was sent. |
| 167 | + UNKNOWN_OPCODE = 4001 |
| 168 | + #: Server failed to decode a payload. |
| 169 | + DECODE_ERROR = 4002 |
| 170 | + #: A payload was sent prior to identifying. |
| 171 | + NOT_AUTHENTICATED = 4003 |
| 172 | + #: The token in the identify payload was incorrect. |
| 173 | + AUTHENTICATION_FAILED = 4004 |
| 174 | + #: More than one identify payload was sent. |
| 175 | + ALREADY_AUTHENTICATED = 4005 |
| 176 | + #: Attempted to resume an invalid session. Now unused, Op 9 is sent instead. |
| 177 | + INVALID_SESSION_RESUMED = 4006 |
| 178 | + #: An invalid sequence was used for resuming. |
| 179 | + INVALID_SEQUENCE = 4007 |
| 180 | + #: We are being rate limited. |
| 181 | + RATE_LIMITED = 4008 |
| 182 | + #: The session timed out. |
| 183 | + SESSION_TIMEOUT = 4009 |
| 184 | + #: Invalid shard was sent in the identify payload. |
| 185 | + INVALID_SHARD = 4010 |
| 186 | + #: Too many guilds were to be handled by a single connection. |
| 187 | + SHARDING_REQUIRED = 4011 |
| 188 | + |
| 189 | + @property |
| 190 | + def name(self) -> str: |
| 191 | + """Returns a human-readable version of the enum member's name.""" |
| 192 | + |
| 193 | + return ' '.join(part.capitalize() for part in self._name_.split('_')) |
| 194 | + |
| 195 | + |
150 | 196 | class ClamorError(Exception): |
151 | 197 | """Base exception class for any exceptions raised by this library. |
152 | 198 |
|
@@ -291,3 +337,39 @@ class Hierarchied(ClamorError): |
291 | 337 | *Even occurs if the bot has ``Kick/Ban Members`` permissions.* |
292 | 338 | """ |
293 | 339 | pass |
| 340 | + |
| 341 | + |
| 342 | +class GatewayError(ClamorError): |
| 343 | + """Base class for every error raised by gateway components. |
| 344 | +
|
| 345 | + Catching this error is not recommended as it mostly |
| 346 | + indicates a client or server panic. |
| 347 | + """ |
| 348 | + pass |
| 349 | + |
| 350 | + |
| 351 | +class EncodingFailed(GatewayError): |
| 352 | + """Raised when the encoding or decoding of a message fails. |
| 353 | +
|
| 354 | + Parameters |
| 355 | + ---------- |
| 356 | + err : Optional[str] |
| 357 | + Error message returned by the encoder |
| 358 | + data : Optional[Union[dict, str]] |
| 359 | + Raw data of the message |
| 360 | + """ |
| 361 | + |
| 362 | + def __init__(self, err: Optional[str], data: Optional[Union[dict, str]] = None): |
| 363 | + if data: |
| 364 | + error = "Encoding of message {} failed".format(str(data)) |
| 365 | + else: |
| 366 | + error = "Decoding of gateway message failed" |
| 367 | + if error: |
| 368 | + error += " with exception {}".format(err) |
| 369 | + |
| 370 | + super().__init__(error) |
| 371 | + |
| 372 | + |
| 373 | +class InvalidListener(GatewayError): |
| 374 | + """Raised by the emitter when a listener is not a coroutine.""" |
| 375 | + pass |
0 commit comments