Skip to content

Commit

Permalink
refactor eventsubws subscription error handling to not error on recon…
Browse files Browse the repository at this point in the history
…nect (#439)

* refactor eventsubws subscription error handling to not error on reconnect

* Why do we still support 3.7

* formatting
  • Loading branch information
IAmTomahawkx committed Mar 15, 2024
1 parent bf3a9f9 commit ce7ac6a
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions twitchio/ext/eventsub/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from twitchio import PartialUser, Unauthorized, HTTPException

if TYPE_CHECKING:
from typing_extensions import Literal
from twitchio import Client

logger = logging.getLogger("twitchio.ext.eventsub.ws")
Expand All @@ -32,7 +33,7 @@ def __init__(self, event_type: Tuple[str, int, Type[models.EventData]], conditio
self.token = token
self.subscription_id: Optional[str] = None
self.cost: Optional[int] = None
self.created: asyncio.Future[Tuple[bool, int]] | None = asyncio.Future()
self.created: asyncio.Future[Tuple[Literal[False], int] | Tuple[Literal[True], None]] | None = asyncio.Future()


_T = TypeVar("_T")
Expand Down Expand Up @@ -117,18 +118,25 @@ async def _subscribe(self, obj: _Subscription) -> dict | None:
try:
resp = await self._http.create_websocket_subscription(obj.event, obj.condition, self._session_id, obj.token)
except HTTPException as e:
assert obj.created
obj.created.set_result((False, e.status)) # type: ignore
if obj.created:
obj.created.set_result((False, e.status))

else:
logger.error(
"An error (%s %s) occurred while attempting to resubscribe to an event on reconnect: %s",
e.status,
e.reason,
e.message,
)

return None

else:
assert obj.created
obj.created.set_result((True, None)) # type: ignore
if obj.created:
obj.created.set_result((True, None))

data = resp["data"][0]
cost = data["cost"]
self.remaining_slots = resp["max_total_cost"] - resp["total_cost"]
obj.cost = cost
obj.cost = data["cost"]

return data

Expand Down

0 comments on commit ce7ac6a

Please sign in to comment.