Skip to content

Commit

Permalink
- Fix rate limit error throwing
Browse files Browse the repository at this point in the history
- Fix SignAPIError message formatting
- Allow failed parses to print an error to the console instead of crashing the client
- Fix shadowing Enum `MessageType` which causes a crash
  • Loading branch information
isaackogan committed Feb 28, 2024
1 parent ffe49ce commit 5faa492
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
8 changes: 7 additions & 1 deletion TikTokLive/client/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import logging
import traceback
import urllib.parse
from asyncio import AbstractEventLoop, Task, CancelledError
from logging import Logger
Expand Down Expand Up @@ -319,7 +320,12 @@ def _parse_webcast_response(self, response: Optional[WebcastResponseMessage]) ->
return [response_event, UnknownEvent().from_pydict(response.to_dict())]

# Get the underlying events
proto_event: ProtoEvent = event_type().parse(response.payload)
try:
proto_event: ProtoEvent = event_type().parse(response.payload)
except Exception:
self._logger.error(traceback.format_exc())
return [response_event]

parsed_events: List[Event] = [response_event, proto_event]
custom_event: Optional[Event] = self._parse_custom_event(response, proto_event)

Expand Down
4 changes: 2 additions & 2 deletions TikTokLive/client/web/routes/fetch_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
self.reason = reason
args = list(args)
args.insert(0, f"[{reason.name}]")
super().__init__(*args)
super().__init__(" ".join(args))


class SignatureRateLimitError(SignAPIError):
Expand All @@ -70,7 +70,7 @@ def __init__(self, retry_after: int, reset_time: int, *args):
_args = list(args)
_args[0] = str(args[0]) % str(self.retry_after)

super().__init__(reason=SignAPIError.ErrorReason.RATE_LIMIT, *args)
super().__init__(SignAPIError.ErrorReason.RATE_LIMIT, *_args)

@property
def retry_after(self) -> int:
Expand Down
7 changes: 5 additions & 2 deletions TikTokLive/proto/custom_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

from TikTokLive.proto import User, GiftStruct

MessageType: Type = TypeVar('MessageType', bound=betterproto.Message)
# "MessageType" is a proto enum field.
# This underscore is the difference between life & death, because if you shadow the proto field,
# everything crashes & burns in a fiery hell.
_MessageType: Type = TypeVar('_MessageType', bound=betterproto.Message)


def proto_extension(cls: MessageType):
def proto_extension(cls: _MessageType):
"""
Betterproto doesn't properly handle inheriting existing messages.
This method takes the superclass proto metadata and assigns that to this one.
Expand Down

0 comments on commit 5faa492

Please sign in to comment.