diff --git a/github/GithubRetry.py b/github/GithubRetry.py index 6ae0eb25..73fb2ec9 100644 --- a/github/GithubRetry.py +++ b/github/GithubRetry.py @@ -112,7 +112,19 @@ class GithubRetry(Retry): content = self.get_content(response, url) content = json.loads(content) message = content.get("message") + except Exception as e: + # we want to fall back to the actual github exception (probably a rate limit error) + # but provide some context why we could not deal with it without another exception + try: + raise RuntimeError( + "Failed to inspect response message" + ) from e + except RuntimeError as e: + raise GithubException( + response.status, content, response.headers + ) from e + try: if Requester.isRateLimitError(message): rate_type = ( "primary" @@ -191,11 +203,16 @@ class GithubRetry(Retry): except (MaxRetryError, GithubException): raise except Exception as e: - self.__log( - logging.WARNING, - "Failed to inspect response message", - exc_info=e, - ) + # we want to fall back to the actual github exception (probably a rate limit error) + # but provide some context why we could not deal with it without another exception + try: + raise RuntimeError( + "Failed to determine retry backoff" + ) from e + except RuntimeError as e: + raise GithubException( + response.status, content, response.headers + ) from e raise GithubException(response.status, content, response.headers) diff --git a/tests/GithubRetry.py b/tests/GithubRetry.py index cf67388c..98e639b3 100644 --- a/tests/GithubRetry.py +++ b/tests/GithubRetry.py @@ -20,6 +20,7 @@ # # ################################################################################ import contextlib +import logging import sys import unittest from datetime import datetime @@ -406,15 +407,17 @@ class GithubRetry(unittest.TestCase): self.assertEqual("NOT GOOD", exp.exception.data) self.assertEqual({}, exp.exception.headers) - self.assertListEqual( - [ - (20, "Request TEST URL failed with 403: NOT GOOD"), - (30, "Failed to inspect response message"), - ], - [call[1] for call in log.mock_calls], - ) + self.assertIsInstance(exp.exception.__cause__, RuntimeError) + self.assertEqual( + ("Failed to inspect response message",), exp.exception.__cause__.args + ) - self.assertListEqual( - [{}, {"exc_info": "Unable to determine whether fp is closed."}], - [{k: str(v) for k, v in call[2].items()} for call in log.mock_calls], + self.assertIsInstance(exp.exception.__cause__.__cause__, ValueError) + self.assertEqual( + ("Unable to determine whether fp is closed.",), + exp.exception.__cause__.__cause__.args, + ) + + log.assert_called_once_with( + logging.INFO, "Request TEST URL failed with 403: NOT GOOD" )