Add better error logging for unexpected errors, add error messages for exceeded YouTube quota (#4552)

This commit is contained in:
jack1142 2020-10-28 12:37:56 +01:00 committed by GitHub
parent c95d526bc6
commit 4aa84a5d22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 13 deletions

View File

@ -18,5 +18,9 @@ class InvalidYoutubeCredentials(StreamsError):
pass
class YoutubeQuotaExceeded(StreamsError):
pass
class OfflineStream(StreamsError):
pass

View File

@ -19,6 +19,7 @@ from .errors import (
OfflineStream,
StreamNotFound,
StreamsError,
YoutubeQuotaExceeded,
)
from . import streamtypes as _streamtypes
@ -262,7 +263,19 @@ class Streams(commands.Cog):
"The YouTube API key is either invalid or has not been set. See {command}."
).format(command=f"`{ctx.clean_prefix}streamset youtubekey`")
)
except APIError:
except YoutubeQuotaExceeded:
await ctx.send(
_(
"YouTube quota has been exceeded."
" Try again later or contact the owner if this continues."
)
)
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,
)
await ctx.send(
_("Something went wrong whilst trying to contact the stream service's API.")
)
@ -412,7 +425,19 @@ class Streams(commands.Cog):
).format(command=f"`{ctx.clean_prefix}streamset youtubekey`")
)
return
except APIError:
except YoutubeQuotaExceeded:
await ctx.send(
_(
"YouTube quota has been exceeded."
" Try again later or contact the owner if this continues."
)
)
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,
)
await ctx.send(
_("Something went wrong whilst trying to contact the stream service's API.")
)

View File

@ -14,6 +14,7 @@ from .errors import (
InvalidTwitchCredentials,
InvalidYoutubeCredentials,
StreamNotFound,
YoutubeQuotaExceeded,
)
from redbot.core.i18n import Translator
from redbot.core.utils.chat_formatting import humanize_number
@ -180,12 +181,16 @@ class YoutubeStream(Stream):
async with session.get(YOUTUBE_CHANNELS_ENDPOINT, params=params) as r:
data = await r.json()
if (
"error" in data
and data["error"]["code"] == 400
and data["error"]["errors"][0]["reason"] == "keyInvalid"
):
if "error" in data:
error_code = data["error"]["code"]
if error_code == 400 and data["error"]["errors"][0]["reason"] == "keyInvalid":
raise InvalidYoutubeCredentials()
elif error_code == 403 and data["error"]["errors"][0]["reason"] in (
"dailyLimitExceeded",
"quotaExceeded",
"rateLimitExceeded",
):
raise YoutubeQuotaExceeded()
elif "items" in data and len(data["items"]) == 0:
raise StreamNotFound()
elif "items" in data:
@ -196,7 +201,7 @@ class YoutubeStream(Stream):
and data["pageInfo"]["totalResults"] < 1
):
raise StreamNotFound()
raise APIError()
raise APIError(data)
def __repr__(self):
return "<{0.__class__.__name__}: {0.name} (ID: {0.id})>".format(self)
@ -276,7 +281,7 @@ class TwitchStream(Stream):
elif r.status == 404:
raise StreamNotFound()
else:
raise APIError()
raise APIError(data)
async def fetch_id(self):
header = {"Client-ID": str(self._client_id)}
@ -298,7 +303,7 @@ class TwitchStream(Stream):
elif r.status == 401:
raise InvalidTwitchCredentials()
else:
raise APIError()
raise APIError(data)
def make_embed(self, data):
is_rerun = data["type"] == "rerun"
@ -347,7 +352,7 @@ class HitboxStream(Stream):
# self.already_online = True
return self.make_embed(data)
raise APIError()
raise APIError(data)
def make_embed(self, data):
base_url = "https://edge.sf.hitbox.tv"
@ -386,7 +391,7 @@ class PicartoStream(Stream):
elif r.status == 404:
raise StreamNotFound()
else:
raise APIError()
raise APIError(data)
def make_embed(self, data):
avatar = rnd(