mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-09 12:30:15 +02:00
Merge branch 'gitlab-api-request-error-handling' into 'v2.9.x'
Added error handling when fetching from GitLab API See merge request allianceauth/allianceauth!1370
This commit is contained in:
commit
2595fa5c51
@ -1,6 +1,7 @@
|
|||||||
from math import ceil
|
from math import ceil
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import requests
|
||||||
import requests_mock
|
import requests_mock
|
||||||
from packaging.version import Version as Pep440Version
|
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)
|
result = _fetch_list_from_gitlab(self.url, max_pages=max_pages)
|
||||||
self.assertEqual(result, GITHUB_TAGS[:4])
|
self.assertEqual(result, GITHUB_TAGS[:4])
|
||||||
self.assertEqual(requests_mocker.call_count, max_pages)
|
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, [])
|
||||||
|
@ -156,14 +156,27 @@ def _latests_versions(tags: list) -> tuple:
|
|||||||
|
|
||||||
|
|
||||||
def _fetch_list_from_gitlab(url: str, max_pages: int = MAX_PAGES) -> list:
|
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()
|
result = list()
|
||||||
|
|
||||||
for page in range(1, max_pages + 1):
|
for page in range(1, max_pages + 1):
|
||||||
request = requests.get(
|
try:
|
||||||
url, params={'page': page}, timeout=REQUESTS_TIMEOUT
|
request = requests.get(
|
||||||
)
|
url, params={'page': page}, timeout=REQUESTS_TIMEOUT
|
||||||
request.raise_for_status()
|
)
|
||||||
|
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()
|
result += request.json()
|
||||||
|
|
||||||
if 'x-total-pages' in request.headers:
|
if 'x-total-pages' in request.headers:
|
||||||
try:
|
try:
|
||||||
total_pages = int(request.headers['x-total-pages'])
|
total_pages = int(request.headers['x-total-pages'])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user