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
  • Loading branch information
IAmTomahawkx committed Jan 15, 2024
1 parent 30a814f commit bdb21b0
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions twitchio/ext/eventsub/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging

import aiohttp
from typing import Optional, TYPE_CHECKING, Tuple, Type, Dict, Callable, Generic, TypeVar, Awaitable, Union, cast, List
from typing import Optional, TYPE_CHECKING, Tuple, Type, Dict, Callable, Generic, TypeVar, Awaitable, Union, cast, List, Literal
from . import models, http
from .models import _loads
from twitchio import PartialUser, Unauthorized, HTTPException
Expand Down Expand Up @@ -32,7 +32,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 +117,20 @@ 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 bdb21b0

Please sign in to comment.