From 9ffa84790330337d52703ea78ae7e4f434a30da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Thu, 28 May 2026 19:17:40 +0300 Subject: [PATCH 1/3] Improve response parsing for OAuthRefreshException - Handle missing, invalid, or non-dict JSON payloads - Avoid crashes when refresh failures return unexpected bodies - Add fallback type safe values for error, error_description Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: OpenCode (gpt-5.4) Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- trakt/errors.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/trakt/errors.py b/trakt/errors.py index a357847..56df876 100644 --- a/trakt/errors.py +++ b/trakt/errors.py @@ -73,7 +73,7 @@ class OAuthRefreshException(OAuthException): def __init__(self, response=None): super().__init__(response) - self.data = self.response.json() + self.data = self._error_data(self.response) @property def error(self): @@ -83,6 +83,30 @@ def error(self): def error_description(self): return self.data["error_description"] + @classmethod + def _error_data(cls, response): + data = cls._response_json(response) + + return { + "error": data.get("error", ""), + "error_description": data.get("error_description", ""), + } + + @staticmethod + def _response_json(response): + if response is None: + return {} + + try: + data = response.json() + except (ValueError, AttributeError): + return {} + + if not isinstance(data, dict): + return {} + + return data + class ForbiddenException(TraktException): """TraktException type to be raised when a 403 return code is received""" From c0566a7fd29010f00b9b85b144c3aad703488a0a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 28 May 2026 17:21:57 +0000 Subject: [PATCH 2/3] Add OAuthRefreshException class docstring --- trakt/errors.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/trakt/errors.py b/trakt/errors.py index 56df876..f90ba69 100644 --- a/trakt/errors.py +++ b/trakt/errors.py @@ -69,6 +69,8 @@ class OAuthException(TraktException): class OAuthRefreshException(OAuthException): + """TraktException type to be raised when OAuth token refresh fails""" + message = 'Unauthorized - OAuth token refresh failed' def __init__(self, response=None): From d65434ca21183b2db80bd8dfb92b66a9dc18aa96 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 28 May 2026 17:22:55 +0000 Subject: [PATCH 3/3] Refine OAuthRefreshException docstring wording --- trakt/errors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trakt/errors.py b/trakt/errors.py index f90ba69..5b366d2 100644 --- a/trakt/errors.py +++ b/trakt/errors.py @@ -69,7 +69,7 @@ class OAuthException(TraktException): class OAuthRefreshException(OAuthException): - """TraktException type to be raised when OAuth token refresh fails""" + """Raised when OAuth token refresh fails.""" message = 'Unauthorized - OAuth token refresh failed'