diff --git a/backend/handler/igdb_handler.py b/backend/handler/igdb_handler.py index 00f218eb1..4f9fe5b44 100644 --- a/backend/handler/igdb_handler.py +++ b/backend/handler/igdb_handler.py @@ -49,9 +49,23 @@ class IGDBHandler: try: res = requests.post(url, data, headers=self.headers, timeout=timeout) res.raise_for_status() + return res.json() except (HTTPError, Timeout) as err: + if err.response.status_code != 401: + log.error(err) + return [] # All requests to the IGDB API return a list + + # Attempt to force a token refresh if the token is invalid + log.warning("Twitch token invalid: fetching a new one...") + token = self.twitch_auth._update_twitch_token() + self.headers["Authorization"] = f"Bearer {token}" + + try: + res = requests.post(url, data, headers=self.headers, timeout=timeout) + res.raise_for_status() + except (HTTPError, Timeout) as err: + # Log the error and return an empty list if the request fails again log.error(err) - # All requests to the IGDB API return a list return [] return res.json()