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):
|
||||
pass
|
||||
|
||||
@ -7,7 +10,13 @@ class StreamNotFound(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):
|
||||
|
||||
@ -766,6 +766,13 @@ class Streams(commands.Cog):
|
||||
|
||||
stream.messages.clear()
|
||||
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:
|
||||
if stream.messages:
|
||||
continue
|
||||
|
||||
@ -274,7 +274,7 @@ class YoutubeStream(Stream):
|
||||
and data["pageInfo"]["totalResults"] < 1
|
||||
):
|
||||
raise StreamNotFound()
|
||||
raise APIError(data)
|
||||
raise APIError(r.status, data)
|
||||
|
||||
def _check_api_errors(self, data: dict):
|
||||
if "error" in data:
|
||||
@ -287,7 +287,7 @@ class YoutubeStream(Stream):
|
||||
"rateLimitExceeded",
|
||||
):
|
||||
raise YoutubeQuotaExceeded()
|
||||
raise APIError(data)
|
||||
raise APIError(error_code, data)
|
||||
|
||||
def __repr__(self):
|
||||
return "<{0.__class__.__name__}: {0.name} (ID: {0.id})>".format(self)
|
||||
@ -394,7 +394,7 @@ class TwitchStream(Stream):
|
||||
elif stream_code == 404:
|
||||
raise StreamNotFound()
|
||||
else:
|
||||
raise APIError(stream_data)
|
||||
raise APIError(stream_code, stream_data)
|
||||
|
||||
async def _fetch_user_profile(self):
|
||||
code, data = await self.get_data(TWITCH_ID_ENDPOINT, {"login": self.name})
|
||||
@ -409,7 +409,7 @@ class TwitchStream(Stream):
|
||||
elif code == 401:
|
||||
raise InvalidTwitchCredentials()
|
||||
else:
|
||||
raise APIError(data)
|
||||
raise APIError(code, data)
|
||||
|
||||
def make_embed(self, data):
|
||||
is_rerun = data["type"] == "rerun"
|
||||
@ -458,7 +458,7 @@ class PicartoStream(Stream):
|
||||
elif r.status == 404:
|
||||
raise StreamNotFound()
|
||||
else:
|
||||
raise APIError(data)
|
||||
raise APIError(r.status, data)
|
||||
|
||||
def make_embed(self, data):
|
||||
avatar = rnd(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user