mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
Include status code in APIError and handle APIError in the loop (#4995)
This commit is contained in:
parent
b89c43eb0f
commit
c4a9d97a4b
@ -1,3 +1,6 @@
|
|||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
class StreamsError(Exception):
|
class StreamsError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -7,7 +10,13 @@ class StreamNotFound(StreamsError):
|
|||||||
|
|
||||||
|
|
||||||
class APIError(StreamsError):
|
class APIError(StreamsError):
|
||||||
pass
|
def __init__(self, status_code: int, raw_data: Any) -> None:
|
||||||
|
self.status_code = status_code
|
||||||
|
self.raw_data = raw_data
|
||||||
|
super().__init__(f"{status_code=} {raw_data=}")
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return f"{self.__class__.__name__}({self!s})"
|
||||||
|
|
||||||
|
|
||||||
class InvalidTwitchCredentials(StreamsError):
|
class InvalidTwitchCredentials(StreamsError):
|
||||||
|
|||||||
@ -766,6 +766,13 @@ class Streams(commands.Cog):
|
|||||||
|
|
||||||
stream.messages.clear()
|
stream.messages.clear()
|
||||||
await self.save_streams()
|
await self.save_streams()
|
||||||
|
except APIError as e:
|
||||||
|
log.error(
|
||||||
|
"Something went wrong whilst trying to contact the stream service's API.\n"
|
||||||
|
"Raw response data:\n%r",
|
||||||
|
e,
|
||||||
|
)
|
||||||
|
continue
|
||||||
else:
|
else:
|
||||||
if stream.messages:
|
if stream.messages:
|
||||||
continue
|
continue
|
||||||
|
|||||||
@ -274,7 +274,7 @@ class YoutubeStream(Stream):
|
|||||||
and data["pageInfo"]["totalResults"] < 1
|
and data["pageInfo"]["totalResults"] < 1
|
||||||
):
|
):
|
||||||
raise StreamNotFound()
|
raise StreamNotFound()
|
||||||
raise APIError(data)
|
raise APIError(r.status, data)
|
||||||
|
|
||||||
def _check_api_errors(self, data: dict):
|
def _check_api_errors(self, data: dict):
|
||||||
if "error" in data:
|
if "error" in data:
|
||||||
@ -287,7 +287,7 @@ class YoutubeStream(Stream):
|
|||||||
"rateLimitExceeded",
|
"rateLimitExceeded",
|
||||||
):
|
):
|
||||||
raise YoutubeQuotaExceeded()
|
raise YoutubeQuotaExceeded()
|
||||||
raise APIError(data)
|
raise APIError(error_code, data)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<{0.__class__.__name__}: {0.name} (ID: {0.id})>".format(self)
|
return "<{0.__class__.__name__}: {0.name} (ID: {0.id})>".format(self)
|
||||||
@ -394,7 +394,7 @@ class TwitchStream(Stream):
|
|||||||
elif stream_code == 404:
|
elif stream_code == 404:
|
||||||
raise StreamNotFound()
|
raise StreamNotFound()
|
||||||
else:
|
else:
|
||||||
raise APIError(stream_data)
|
raise APIError(stream_code, stream_data)
|
||||||
|
|
||||||
async def _fetch_user_profile(self):
|
async def _fetch_user_profile(self):
|
||||||
code, data = await self.get_data(TWITCH_ID_ENDPOINT, {"login": self.name})
|
code, data = await self.get_data(TWITCH_ID_ENDPOINT, {"login": self.name})
|
||||||
@ -409,7 +409,7 @@ class TwitchStream(Stream):
|
|||||||
elif code == 401:
|
elif code == 401:
|
||||||
raise InvalidTwitchCredentials()
|
raise InvalidTwitchCredentials()
|
||||||
else:
|
else:
|
||||||
raise APIError(data)
|
raise APIError(code, data)
|
||||||
|
|
||||||
def make_embed(self, data):
|
def make_embed(self, data):
|
||||||
is_rerun = data["type"] == "rerun"
|
is_rerun = data["type"] == "rerun"
|
||||||
@ -458,7 +458,7 @@ class PicartoStream(Stream):
|
|||||||
elif r.status == 404:
|
elif r.status == 404:
|
||||||
raise StreamNotFound()
|
raise StreamNotFound()
|
||||||
else:
|
else:
|
||||||
raise APIError(data)
|
raise APIError(r.status, data)
|
||||||
|
|
||||||
def make_embed(self, data):
|
def make_embed(self, data):
|
||||||
avatar = rnd(
|
avatar = rnd(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user