phabricator: Ensure request errors are raised as exceptions

This ensures that a celery task will be marked as failed if a request error
happens when listing origins.
This commit is contained in:
Antoine Lambert 2021-01-14 17:23:07 +01:00
parent b743c36496
commit a41c03e4c8
2 changed files with 26 additions and 3 deletions

View file

@ -91,7 +91,7 @@ class PhabricatorLister(StatelessLister[PageType]):
while True:
params = self.get_request_params(after)
logger.debug(
"Retrieving results on URI=%s, parameters %s",
"Retrieving results on URI %s with parameters %s",
self.url,
self.filter_params(params),
)
@ -99,12 +99,13 @@ class PhabricatorLister(StatelessLister[PageType]):
if response.status_code != 200:
logger.warning(
"Got unexpected status_code %s on %s: %s",
"Unexpected HTTP status code %s on %s: %s",
response.status_code,
response.url,
response.content,
)
break
response.raise_for_status()
response_data = response.json()

View file

@ -7,6 +7,7 @@ import json
from pathlib import Path
import pytest
from requests.exceptions import HTTPError
from swh.lister import USER_AGENT
from swh.lister.phabricator.lister import PhabricatorLister, get_repo_url
@ -111,3 +112,24 @@ def test_lister(
scheduler_origins = swh_scheduler.get_listed_origins(lister.lister_obj.id).origins
assert len(scheduler_origins) == expected_nb_origins
def test_lister_request_error(
swh_scheduler, requests_mock, phabricator_repositories_page1,
):
FORGE_BASE_URL = "https://forge.softwareheritage.org"
lister = PhabricatorLister(
scheduler=swh_scheduler, url=FORGE_BASE_URL, instance="swh", api_token="foo"
)
requests_mock.post(
f"{FORGE_BASE_URL}{lister.API_REPOSITORY_PATH}",
[
{"status_code": 200, "json": phabricator_repositories_page1},
{"status_code": 500, "reason": "Internal Server Error"},
],
)
with pytest.raises(HTTPError):
lister.run()