Exceptions cleanup

This commit is contained in:
DevilXD committed 2024-08-04 15:08:40 +02:00
1 parent 9779d7a2c8
commit 4dcabeeb9e
2 files changed
+22 -12

No files matched your search

+8
View File
@@ -84,3 +84,11 @@ class CaptchaRequired(LoginException):
""" """
def __init__(self): def __init__(self):
super().__init__("Captcha is required") super().__init__("Captcha is required")
class GQLException(RequestException):
"""
Raised when a GQL request returns an error response.
"""
def __init__(self, message: str):
super().__init__(message)
+14 -12
View File
@@ -21,12 +21,14 @@ from channel import Channel
from websocket import WebsocketPool from websocket import WebsocketPool
from inventory import DropsCampaign from inventory import DropsCampaign
from exceptions import ( from exceptions import (
MinerException,
CaptchaRequired,
ExitRequest, ExitRequest,
LoginException, GQLException,
ReloadRequest, ReloadRequest,
LoginException,
MinerException,
RequestInvalid, RequestInvalid,
CaptchaRequired,
RequestException,
) )
from utils import ( from utils import (
CHARS_HEX_LOWER, CHARS_HEX_LOWER,
@@ -42,8 +44,8 @@ from constants import (
CALL, CALL,
DUMP_PATH, DUMP_PATH,
COOKIES_PATH, COOKIES_PATH,
GQL_OPERATIONS,
MAX_CHANNELS, MAX_CHANNELS,
GQL_OPERATIONS,
WATCH_INTERVAL, WATCH_INTERVAL,
State, State,
ClientType, ClientType,
@@ -315,7 +317,7 @@ class _AuthState:
if hasattr(self, "access_token"): if hasattr(self, "access_token"):
return self.access_token return self.access_token
raise MinerException("Login flow finished without setting the access token") raise LoginException("Login flow finished without setting the access token")
def headers(self, *, user_agent: str = '', gql: bool = False) -> JsonType: def headers(self, *, user_agent: str = '', gql: bool = False) -> JsonType:
client_info: ClientInfo = self._twitch._client_type client_info: ClientInfo = self._twitch._client_type
@@ -397,7 +399,7 @@ class _AuthState:
else: else:
raise RuntimeError("Login verification failure") raise RuntimeError("Login verification failure")
if validate_response["client_id"] != client_info.CLIENT_ID: if validate_response["client_id"] != client_info.CLIENT_ID:
raise MinerException("You're using an old cookie file, please generate a new one.") raise LoginException("You're using an old cookie file, please generate a new one.")
self.user_id = int(validate_response["user_id"]) self.user_id = int(validate_response["user_id"])
cookie["persistent"] = str(self.user_id) cookie["persistent"] = str(self.user_id)
logger.info(f"Login successful, user ID: {self.user_id}") logger.info(f"Login successful, user ID: {self.user_id}")
@@ -574,7 +576,7 @@ class Twitch:
except ExitRequest: except ExitRequest:
break break
except aiohttp.ContentTypeError as exc: except aiohttp.ContentTypeError as exc:
raise MinerException(_("login", "unexpected_content")) from exc raise RequestException(_("login", "unexpected_content")) from exc
async def _run(self): async def _run(self):
""" """
@@ -1348,18 +1350,18 @@ class Twitch:
force_retry = True force_retry = True
break break
else: else:
raise MinerException(f"GQL error: {response_json['errors']}") raise GQLException(response_json['errors'])
# Other error handling # Other error handling
elif "error" in response_json: elif "error" in response_json:
raise MinerException( raise GQLException(
f"GQL error: {response_json['error']}: {response_json['message']}" f"{response_json['error']}: {response_json['message']}"
) )
if force_retry: if force_retry:
break break
else: else:
return orig_response return orig_response
await asyncio.sleep(delay) await asyncio.sleep(delay)
raise MinerException() raise GQLException("Retry loop was broken")
def _merge_data(self, primary_data: JsonType, secondary_data: JsonType) -> JsonType: def _merge_data(self, primary_data: JsonType, secondary_data: JsonType) -> JsonType:
merged = {} merged = {}
@@ -1544,7 +1546,7 @@ class Twitch:
}, },
}) })
) )
except MinerException as exc: except GQLException as exc:
raise MinerException(f"Game: {game.slug}") from exc raise MinerException(f"Game: {game.slug}") from exc
if "game" in response["data"]: if "game" in response["data"]:
return [ return [