Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve codelists check behaviour if opencodelists.org is down #297

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Handle more errors from opencodelists
  • Loading branch information
Jongmassey committed Dec 31, 2024
commit 258422f4b1f1f306cc407d9659673ee9bd7ad1e0
33 changes: 22 additions & 11 deletions opensafely/codelists.py
Original file line number Diff line number Diff line change
@@ -126,25 +126,29 @@ def check_upstream(codelists_dir=None):
"manifest": manifest_file.read_text(),
}
url = f"{OPENCODELISTS_BASE_URL}/api/v1/check/"
response = requests.post(url, post_data).json()
status = response["status"]
response = requests.post(url, post_data)
response.raise_for_status()
response_json = response.json()
if "status" not in response_json:
raise ValueError("status not found in response")
status = response_json["status"]

if status == "error":
# The OpenCodelists check endpoint returns an error in the response data if it
# encounters an invalid user, organisation or codelist in the codelists.txt file, or
# if any codelists in codelists.csv don't match the expected pattern. These should all
# be fixable by `opensafely codelists update`.
if "error" in response["data"]:
if "error" in response_json["data"]:
error_message = (
f"Error checking upstream codelists: {response['data']['error']}\n"
f"Error checking upstream codelists: {response_json['data']['error']}\n"
)
elif response["data"]["added"] or response["data"]["removed"]:
elif response_json["data"]["added"] or response_json["data"]["removed"]:
error_message = (
"Codelists have been added or removed\n\n"
"For details, run:\n\n opensafely codelists check\n"
)
else:
changed = "\n ".join(response["data"]["changed"])
changed = "\n ".join(response_json["data"]["changed"])
error_message = (
f"Some codelists are out of date\nCodelists affected:\n {changed}\n"
)
@@ -221,11 +225,18 @@ def check():

try:
check_upstream(codelists_dir)
except requests.exceptions.ConnectionError:
print(
f"Local codelists OK; could not contact {OPENCODELISTS_BASE_URL} for upstream check,"
"try again later"
)
except (
requests.exceptions.ConnectionError,
requests.exceptions.HTTPError,
ValueError,
) as e:
if e is ValueError and e.message != "status not found in response":
raise e
else:
print(
f"Local codelists OK; could not contact {OPENCODELISTS_BASE_URL} for upstream check,"
"try again later"
)
return True


18 changes: 18 additions & 0 deletions tests/test_codelists.py
Original file line number Diff line number Diff line change
@@ -87,6 +87,24 @@ def test_codelists_check_passes_if_opencodelists_is_down(requests_mock, codelist
assert codelists.check()


def test_codelists_check_passes_if_opencodelists_return_lacks_status(
mock_check, codelists_path
):
mock_check(response={"test": "ok"})
os.chdir(codelists_path)
assert codelists.check()


def test_codelists_check_passes_if_opencodelists_returns_error(
requests_mock, codelists_path
):
requests_mock.post(
"https://www.opencodelists.org/api/v1/check/", exc=requests.exceptions.HTTPError
)
os.chdir(codelists_path)
assert codelists.check()


def test_codelists_check_fail_if_list_updated(codelists_path):
with open(codelists_path / "codelists/codelists.txt", "a") as f:
f.write("\nsomeproject/somelist/someversion")
Loading