Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
15 changes: 14 additions & 1 deletion src/blueapi/utils/numtracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,20 @@ async def create_scan(
json = response.json()

if json.get("errors") is not None:
raise RuntimeError(f"Numtracker error: {json['errors']}")
for error in json["errors"]:
code = error.get("extensions", {}).get("code")
match code:
case "AUTH_FAILED":
raise RuntimeError(
f"Not authorised to create a scan number for "
f"{instrument} and {instrument_session}"
)
case "AUTH_MISSING":
raise RuntimeError("Numtracker authentication missing")
case "AUTH_SERVER_ERROR":
raise RuntimeError("Server authentication error")
Comment thread
shree-iyengar-dls marked this conversation as resolved.
Outdated
case _:
raise RuntimeError(f"Numtracker error: {json['errors']}")
Comment thread
shree-iyengar-dls marked this conversation as resolved.
Outdated

new_collection = NumtrackerScanMutationResponse.model_validate(json["data"])
LOGGER.debug("New NumtrackerNewScan: %s", new_collection)
Expand Down
38 changes: 38 additions & 0 deletions tests/unit_tests/utils/test_numtracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,41 @@ async def test_create_scan_raises_runtime_error_on_graphql_error(
)
with pytest.raises(RuntimeError, match="Numtracker error:"):
await numtracker.create_scan("ab123", "p46")


@pytest.mark.parametrize(
("auth_error_code", "expected_message"),
[
("AUTH_FAILED", "Not authorised to create a scan number for p46 and ab123"),
("AUTH_MISSING", "Numtracker authentication missing"),
("AUTH_SERVER_ERROR", "Server authentication error"),
("UNKWNOWN_ELSE", "Numtracker error:"),
Comment thread
shree-iyengar-dls marked this conversation as resolved.
Outdated
],
)
async def test_numtracker_auth_error_types(
numtracker: NumtrackerClient,
httpx_mock: HTTPXMock,
nt_query,
auth_error_code,
expected_message,
):
error_response = {
"data": None,
"errors": [
{
"message": "xyz",
"locations": [{"line": 3, "column": 5}],
"path": ["scan"],
"extensions": {"code": auth_error_code},
}
],
}
httpx_mock.add_response(
method="POST",
url=URL,
match_json=nt_query,
status_code=200,
json=error_response,
)
with pytest.raises(RuntimeError, match=expected_message):
await numtracker.create_scan("ab123", "p46")
Loading