diff --git a/allianceauth/authentication/tests/test_templatetags.py b/allianceauth/authentication/tests/test_templatetags.py index 9a7a313c..50361724 100644 --- a/allianceauth/authentication/tests/test_templatetags.py +++ b/allianceauth/authentication/tests/test_templatetags.py @@ -1,6 +1,7 @@ from math import ceil from unittest.mock import patch +import requests import requests_mock from packaging.version import Version as Pep440Version @@ -307,3 +308,25 @@ class TestFetchListFromGitlab(TestCase): result = _fetch_list_from_gitlab(self.url, max_pages=max_pages) self.assertEqual(result, GITHUB_TAGS[:4]) self.assertEqual(requests_mocker.call_count, max_pages) + + @requests_mock.mock() + @patch(MODULE_PATH + '.admin_status.logger') + def test_should_not_raise_any_exception_from_github_request_but_log_as_warning( + self, requests_mocker, mock_logger + ): + for my_exception in [ + requests.exceptions.ConnectionError, + requests.exceptions.HTTPError, + requests.exceptions.URLRequired, + requests.exceptions.TooManyRedirects, + requests.exceptions.ConnectTimeout, + requests.exceptions.Timeout, + + ]: + requests_mocker.get(self.url, exc=my_exception) + try: + result = _fetch_list_from_gitlab(self.url) + except Exception as ex: + self.fail(f"Unexpected exception raised: {ex}") + self.assertTrue(mock_logger.warning.called) + self.assertListEqual(result, []) diff --git a/allianceauth/templatetags/admin_status.py b/allianceauth/templatetags/admin_status.py index 9170b168..f7de2513 100644 --- a/allianceauth/templatetags/admin_status.py +++ b/allianceauth/templatetags/admin_status.py @@ -156,14 +156,27 @@ def _latests_versions(tags: list) -> tuple: def _fetch_list_from_gitlab(url: str, max_pages: int = MAX_PAGES) -> list: - """returns a list from the GitLab API. Supports pageing""" + """returns a list from the GitLab API. Supports paging""" result = list() + for page in range(1, max_pages + 1): - request = requests.get( - url, params={'page': page}, timeout=REQUESTS_TIMEOUT - ) - request.raise_for_status() + try: + request = requests.get( + url, params={'page': page}, timeout=REQUESTS_TIMEOUT + ) + request.raise_for_status() + except requests.exceptions.RequestException as e: + error_str = str(e) + + logger.warning( + f'Unable to fetch from GitLab API. Error: {error_str}', + exc_info=True, + ) + + return result + result += request.json() + if 'x-total-pages' in request.headers: try: total_pages = int(request.headers['x-total-pages'])