Skip to content

Commit abbca76

Browse files
committed
Fix spelling and some unit tests, refactor gateway package
1 parent ea3c32d commit abbca76

16 files changed

+466
-365
lines changed

clamor/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
:license: MIT, see LICENSE for more details.
1111
"""
1212

13+
from .gateway import *
1314
from .meta import *
1415
from .rest import *
15-
from .gateway import *
16+
from .utils import *
1617

1718
import logging
1819

clamor/exceptions.py

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
from asks.response_objects import Response
99

1010
__all__ = (
11-
'JSONErrorCode',
1211
'ClamorError',
13-
'RequestFailed',
14-
'Unauthorized',
12+
'EncodingFailed',
1513
'Forbidden',
16-
'NotFound',
14+
'GatewayCloseCode',
15+
'GatewayError',
1716
'Hierarchied',
17+
'InvalidListener',
18+
'JSONErrorCode',
19+
'NotFound',
20+
'RequestFailed',
21+
'Unauthorized',
1822
)
1923

2024
logger = logging.getLogger(__name__)
@@ -147,6 +151,48 @@ def name(self) -> str:
147151
return ' '.join(part.capitalize() for part in self._name_.split('_'))
148152

149153

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+
150196
class ClamorError(Exception):
151197
"""Base exception class for any exceptions raised by this library.
152198
@@ -291,3 +337,39 @@ class Hierarchied(ClamorError):
291337
*Even occurs if the bot has ``Kick/Ban Members`` permissions.*
292338
"""
293339
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

clamor/gateway/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .connector import *
2-
from .exceptions import *
2+
from .encoding import *
33
from .opcodes import *
4+
from .serialization import *

0 commit comments

Comments
 (0)