From d860d0167cd0c3047f6aa15a0a3682dc55c179d2 Mon Sep 17 00:00:00 2001 From: simonc56 Date: Sun, 13 Jul 2025 14:50:11 +0200 Subject: [PATCH 1/3] log error when refresh token fails --- trakt/api.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/trakt/api.py b/trakt/api.py index e730631..6f17bd2 100644 --- a/trakt/api.py +++ b/trakt/api.py @@ -10,7 +10,7 @@ from trakt import errors from trakt.config import AuthConfig from trakt.core import TIMEOUT -from trakt.errors import BadResponseException, OAuthException +from trakt.errors import BadRequestException, BadResponseException, OAuthException __author__ = 'Elan Ruusamäe' @@ -249,10 +249,12 @@ def refresh_token(self): try: response = self.client.post('oauth/token', data) self.refresh_attempts = 0 - except OAuthException: - self.logger.debug( - "Rejected - Unable to refresh expired OAuth token, " - "refresh_token is invalid" + except (OAuthException, BadRequestException) as e: + error = e.response.json().get("error") if e.response is not None else "No error description" + error_description = e.response.json().get("error_description") if e.response is not None else "" + self.logger.error( + "{} - Unable to refresh expired OAuth token ".format(e.http_code) + + "({}) {}".format(error, error_description) ) return From c6eee75486132edf69689e5c06bb36823b570dd5 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 13 Jul 2025 15:07:41 +0200 Subject: [PATCH 2/3] string placeholders in log Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- trakt/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/trakt/api.py b/trakt/api.py index 6f17bd2..97e3aff 100644 --- a/trakt/api.py +++ b/trakt/api.py @@ -253,8 +253,8 @@ def refresh_token(self): error = e.response.json().get("error") if e.response is not None else "No error description" error_description = e.response.json().get("error_description") if e.response is not None else "" self.logger.error( - "{} - Unable to refresh expired OAuth token ".format(e.http_code) + - "({}) {}".format(error, error_description) + "%s - Unable to refresh expired OAuth token (%s) %s", + e.http_code, error, error_description ) return From ee7dd4fa53883f038eafeb578e912bc473a6712b Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 13 Jul 2025 15:10:38 +0200 Subject: [PATCH 3/3] catch JSONDecodeError if response body is not json Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- trakt/api.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/trakt/api.py b/trakt/api.py index 97e3aff..cb06a75 100644 --- a/trakt/api.py +++ b/trakt/api.py @@ -250,8 +250,16 @@ def refresh_token(self): response = self.client.post('oauth/token', data) self.refresh_attempts = 0 except (OAuthException, BadRequestException) as e: - error = e.response.json().get("error") if e.response is not None else "No error description" - error_description = e.response.json().get("error_description") if e.response is not None else "" + if e.response is not None: + try: + error = e.response.json().get("error") + error_description = e.response.json().get("error_description") + except JSONDecodeError: + error = "Invalid JSON response" + error_description = e.response.text + else: + error = "No error description" + error_description = "" self.logger.error( "%s - Unable to refresh expired OAuth token (%s) %s", e.http_code, error, error_description