Skip to content

Commit

Permalink
Propagate authentication errors to end user when calling run() (#345)
Browse files Browse the repository at this point in the history
* propagate authentication errors to end user

* run black

* add changelog entry
  • Loading branch information
IAmTomahawkx committed Oct 24, 2022
1 parent 1c7a29d commit d53d825
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Master
- This is dispatched when the bot fails to join a channel
- This also makes the channel join error message in logs optional
- Bug fixes
- Fix AuthenticationError not being properly propagated when a bad token is given
- Fix channel join failures causing `ValueError: list.remove(x): x not in list` when joining channels after the initial start
- Added :attr:`~twitchio.Chatter.is_vip` property to Chatter
- New PartialUser methods
Expand Down
5 changes: 3 additions & 2 deletions twitchio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion twitchio/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d53d825

Please sign in to comment.