From 2623d3993edb10c047e107b399e3b5bb08746945 Mon Sep 17 00:00:00 2001 From: IAmTomahawkx Date: Sat, 22 Oct 2022 21:40:23 -0700 Subject: [PATCH] propagate authentication errors to end user --- twitchio/client.py | 5 +++-- twitchio/websocket.py | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/twitchio/client.py b/twitchio/client.py index e5915e8d..e16a4f81 100644 --- a/twitchio/client.py +++ b/twitchio/client.py @@ -30,7 +30,7 @@ import sys from typing import Union, Callable, List, Optional, Tuple, Any, Coroutine, Dict -from twitchio.errors import HTTPException +from twitchio.errors import HTTPException, AuthenticationError from . import models from .websocket import WSConnection from .http import TwitchHTTP @@ -155,7 +155,8 @@ def run(self): connects to the twitch IRC server, and cleans up when done. """ try: - self.loop.create_task(self.connect()) + task = self.loop.create_task(self.connect()) + self.loop.run_until_complete(task) # this'll raise if the connect fails self.loop.run_forever() except KeyboardInterrupt: pass diff --git a/twitchio/websocket.py b/twitchio/websocket.py index 1cd4cfca..8a545798 100644 --- a/twitchio/websocket.py +++ b/twitchio/websocket.py @@ -122,7 +122,12 @@ async def _connect(self): if self.is_alive: await self._websocket.close() # If for some reason we are in a weird state, close it before retrying. if not self._client._http.nick: - data = await self._client._http.validate(token=self._token) + try: + data = await self._client._http.validate(token=self._token) + except AuthenticationError: + await self._client._http.session.close() + self._client._closing.set() # clean up and error out (this is called to avoid calling Client.close in start() + raise self.nick = data["login"] self.user_id = int(data["user_id"]) else: