-
Notifications
You must be signed in to change notification settings - Fork 14
refactor: improve numtracker error messages #1538
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,6 +105,14 @@ async def create_scan( | |
| json = response.json() | ||
|
|
||
| if json.get("errors") is not None: | ||
| for error in json["errors"]: | ||
| code = (error.get("extensions") or {}).get("code") | ||
|
|
||
| if code == "AUTH_FAILED": | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to improve the error message for the other cases while we're here? Could be a good use for match - something like match code:
case "AUTH_FAILED":
raise ...
case "AUTH_MISSING":
raise ...
case "AUTH_SERVER_ERROR":
raise ...and then the fall-through case could be the existing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yuss that's a good idea thanks |
||
| raise RuntimeError( | ||
| f"Not authorised to create a scan number for " | ||
| f"{instrument} and {instrument_session}." | ||
| ) | ||
| raise RuntimeError(f"Numtracker error: {json['errors']}") | ||
|
|
||
| new_collection = NumtrackerScanMutationResponse.model_validate(json["data"]) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,18 @@ def numtracker() -> NumtrackerClient: | |
| ], | ||
| } | ||
|
|
||
| AUTH_ERROR = { | ||
| "data": None, | ||
| "errors": [ | ||
| { | ||
| "message": "No configuration available for instrument p46", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. really minor but having the message match the error might prevent confusion later on |
||
| "locations": [{"line": 3, "column": 5}], | ||
| "path": ["scan"], | ||
| "extensions": {"code": "AUTH_FAILED"}, | ||
| } | ||
| ], | ||
| } | ||
|
|
||
|
|
||
| async def test_create_scan( | ||
| numtracker: NumtrackerClient, httpx_mock: HTTPXMock, nt_query, nt_response | ||
|
|
@@ -109,3 +121,19 @@ async def test_create_scan_raises_runtime_error_on_graphql_error( | |
| ) | ||
| with pytest.raises(RuntimeError, match="Numtracker error:"): | ||
| await numtracker.create_scan("ab123", "p46") | ||
|
|
||
|
|
||
| async def test_create_scan_raises_runtime_auth_error_on_graphql_error( | ||
| numtracker: NumtrackerClient, httpx_mock: HTTPXMock, nt_query | ||
| ): | ||
| httpx_mock.add_response( | ||
| method="POST", | ||
| url=URL, | ||
| match_json=nt_query, | ||
| status_code=200, | ||
| json=AUTH_ERROR, | ||
| ) | ||
| with pytest.raises( | ||
| RuntimeError, match="Not authorised to create a scan number for p46 and ab123" | ||
| ): | ||
| await numtracker.create_scan("ab123", "p46") | ||
Uh oh!
There was an error while loading. Please reload this page.