Skip to content

Commit

Permalink
Merge pull request #58 from Sebi94nbg/twitchlive-add-indexerror-try-e…
Browse files Browse the repository at this point in the history
…xcept

Add try-except to better handle invalid API responses
  • Loading branch information
Sebbo94BY authored May 14, 2023
2 parents 9123822 + c65f98b commit 6781550
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions modules/twitch_live/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ def get_twitch_streamer_user_id(self, client_description):
try:
with request.urlopen(api_request) as api_response:
api_response = json.load(api_response)
twitch_streamer_user_id = api_response["data"][0]["id"]
except error.HTTPError as http_error:
# HTTP 400: Bad Request
if int(http_error.code) == 400:
Expand All @@ -241,8 +240,19 @@ def get_twitch_streamer_user_id(self, client_description):
TwitchLive.logger.exception("Failed to get a Twitch streamer user ID.")
raise

try:
twitch_streamer_user_id = api_response["data"][0]["id"]
except IndexError:
TwitchLive.logger.error(
"Received an unexpected API response for the client description `%s`: %s",
str(client_description),
str(api_response),
)
return twitch_streamer_user_id

TwitchLive.logger.debug(
"Got a Twitch streamer user ID: %s.", int(twitch_streamer_user_id)
"Got the following Twitch streamer user ID: %s.",
int(twitch_streamer_user_id),
)

return twitch_streamer_user_id
Expand Down

0 comments on commit 6781550

Please sign in to comment.