Skip to content

Commit ce7ac6a

Browse files
authored
refactor eventsubws subscription error handling to not error on reconnect (#439)
* refactor eventsubws subscription error handling to not error on reconnect * Why do we still support 3.7 * formatting
1 parent bf3a9f9 commit ce7ac6a

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

twitchio/ext/eventsub/websocket.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from twitchio import PartialUser, Unauthorized, HTTPException
1111

1212
if TYPE_CHECKING:
13+
from typing_extensions import Literal
1314
from twitchio import Client
1415

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

3738

3839
_T = TypeVar("_T")
@@ -117,18 +118,25 @@ async def _subscribe(self, obj: _Subscription) -> dict | None:
117118
try:
118119
resp = await self._http.create_websocket_subscription(obj.event, obj.condition, self._session_id, obj.token)
119120
except HTTPException as e:
120-
assert obj.created
121-
obj.created.set_result((False, e.status)) # type: ignore
121+
if obj.created:
122+
obj.created.set_result((False, e.status))
123+
124+
else:
125+
logger.error(
126+
"An error (%s %s) occurred while attempting to resubscribe to an event on reconnect: %s",
127+
e.status,
128+
e.reason,
129+
e.message,
130+
)
131+
122132
return None
123133

124-
else:
125-
assert obj.created
126-
obj.created.set_result((True, None)) # type: ignore
134+
if obj.created:
135+
obj.created.set_result((True, None))
127136

128137
data = resp["data"][0]
129-
cost = data["cost"]
130138
self.remaining_slots = resp["max_total_cost"] - resp["total_cost"]
131-
obj.cost = cost
139+
obj.cost = data["cost"]
132140

133141
return data
134142

0 commit comments

Comments
 (0)