Skip to content

Commit 1374dc7

Browse files
committed
fix: Address PR review comments
1 parent 3314a08 commit 1374dc7

6 files changed

Lines changed: 29 additions & 31 deletions

File tree

src/blueapi/cli/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def on_event(event: AnyEvent) -> None:
358358
except BlueskyStreamingError as se:
359359
raise ClickException(f"streaming error: {se}") from se
360360
except BlueskyRemoteControlError as e:
361-
raise ClickException(f"remote control error: {e.args[1]}") from e
361+
raise ClickException(f"remote control error: {e.args[0]}") from e
362362
except ValueError as ve:
363363
raise ClickException(f"task could not run: {ve}") from ve
364364

src/blueapi/client/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ def inner_on_event(event: AnyEvent, ctx: MessageContext) -> None:
498498
if event.task_status is None:
499499
complete.set_exception(
500500
BlueskyRemoteControlError(
501-
message="Server completed without task status"
501+
"Server completed without task status"
502502
)
503503
)
504504
else:
@@ -530,7 +530,7 @@ def create_and_start_task(self, task: TaskRequest) -> TaskResponse:
530530
return response
531531
else:
532532
raise BlueskyRemoteControlError(
533-
message=f"Tried to create and start task {response.task_id} "
533+
f"Tried to create and start task {response.task_id} "
534534
f"but {worker_response.task_id} was started instead"
535535
)
536536

@@ -659,7 +659,7 @@ def reload_environment(
659659
status = self._rest.delete_environment()
660660
except Exception as e:
661661
raise BlueskyRemoteControlError(
662-
message="Failed to tear down the environment"
662+
"Failed to tear down the environment"
663663
) from e
664664
return self._wait_for_reload(
665665
status,
@@ -684,7 +684,7 @@ def _wait_for_reload(
684684
status = self._rest.get_environment()
685685
if status.error_message is not None:
686686
raise BlueskyRemoteControlError(
687-
message=f"Error reloading environment: {status.error_message}"
687+
f"Error reloading environment: {status.error_message}"
688688
)
689689
elif (
690690
status.initialized and status.environment_id != previous_environment_id

src/blueapi/client/rest.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class UnauthorisedAccessError(BlueskyRequestError):
4242
pass
4343

4444

45-
class BlueskyRemoteControlError(BlueskyRequestError):
45+
class BlueskyRemoteControlError(Exception):
4646
pass
4747

4848

@@ -109,21 +109,21 @@ def _exception(response: requests.Response) -> Exception | None:
109109
if code < 400:
110110
return None
111111
elif code in (401, 403):
112-
return UnauthorisedAccessError(code, str(response))
112+
return UnauthorisedAccessError(code, response.text)
113113
elif code == 404:
114-
return NotFoundError(code, str(response))
114+
return NotFoundError(code, response.text)
115115
else:
116-
return BlueskyRemoteControlError(code, str(response))
116+
return BlueskyRemoteControlError(response.text)
117117

118118

119119
def _create_task_exceptions(response: requests.Response) -> Exception | None:
120120
code = response.status_code
121121
if code < 400:
122122
return None
123123
elif code == 401 or code == 403:
124-
return UnauthorisedAccessError(code, str(response))
124+
return UnauthorisedAccessError(code, response.text)
125125
elif code == 404:
126-
return UnknownPlanError(code, str(response))
126+
return UnknownPlanError(code, response.text)
127127
elif code == 422:
128128
try:
129129
content = response.json()
@@ -135,9 +135,9 @@ def _create_task_exceptions(response: requests.Response) -> Exception | None:
135135
except Exception:
136136
# If the error can't be parsed into something sensible, return the
137137
# raw text in a generic exception so at least it gets reported
138-
return BlueskyRequestError(code, str(response))
138+
return BlueskyRequestError(code, response.text)
139139
else:
140-
return BlueskyRequestError(code, str(response))
140+
return BlueskyRequestError(code, response.text)
141141

142142

143143
class BlueapiRestClient:

tests/system_tests/test_blueapi_system.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def test_cannot_access_endpoints(
219219
"get_oidc_config"
220220
) # get_oidc_config can be accessed without auth
221221
for get_method in blueapi_rest_client_get_methods:
222-
with pytest.raises(UnauthorisedAccessError, match=r"<Response \[401\]>"):
222+
with pytest.raises(UnauthorisedAccessError, match=r"Not authenticated"):
223223
getattr(client_without_auth._rest, get_method)()
224224

225225

@@ -245,7 +245,7 @@ def test_get_plans_by_name(client: BlueapiClient, expected_plans: PlanResponse):
245245

246246

247247
def test_get_non_existent_plan(rest_client: BlueapiRestClient):
248-
with pytest.raises(NotFoundError):
248+
with pytest.raises(NotFoundError, match=r"Item not found"):
249249
rest_client.get_plan("Not exists")
250250

251251

@@ -270,12 +270,12 @@ def test_get_device_by_name(
270270

271271

272272
def test_get_non_existent_device(rest_client: BlueapiRestClient):
273-
with pytest.raises(NotFoundError):
273+
with pytest.raises(NotFoundError, match=r"Item not found"):
274274
rest_client.get_device("Not exists")
275275

276276

277277
def test_client_non_existent_device(client: BlueapiClient):
278-
with pytest.raises(AttributeError):
278+
with pytest.raises(AttributeError, match="No device named 'missing' available"):
279279
_ = client.devices.missing
280280

281281

@@ -297,7 +297,7 @@ def test_instrument_session_propagated(rest_client: BlueapiRestClient):
297297

298298

299299
def test_create_task_validation_error(rest_client: BlueapiRestClient):
300-
with pytest.raises(BlueskyRequestError):
300+
with pytest.raises(BlueskyRequestError, match="Internal Server Error"):
301301
rest_client.create_task(
302302
TaskRequest(
303303
name="Not-exists",
@@ -338,12 +338,12 @@ def test_get_task_by_id(rest_client: BlueapiRestClient):
338338

339339

340340
def test_get_non_existent_task(rest_client: BlueapiRestClient):
341-
with pytest.raises(NotFoundError):
341+
with pytest.raises(NotFoundError, match=r"Item not found"):
342342
rest_client.get_task("Not-exists")
343343

344344

345345
def test_delete_non_existent_task(rest_client: BlueapiRestClient):
346-
with pytest.raises(NotFoundError):
346+
with pytest.raises(NotFoundError, match=r"Item not found"):
347347
rest_client.clear_task("Not-exists")
348348

349349

@@ -365,7 +365,7 @@ def test_put_worker_task_fails_if_not_idle(rest_client: BlueapiRestClient):
365365

366366
with pytest.raises(BlueskyRemoteControlError) as exception:
367367
rest_client.update_worker_task(WorkerTask(task_id=small_task.task_id))
368-
assert "<Response [409]>" in str(exception)
368+
assert "Worker already active" in exception.value.args[0]
369369
rest_client.cancel_current_task(WorkerState.ABORTING)
370370
rest_client.clear_task(small_task.task_id)
371371
rest_client.clear_task(long_task.task_id)
@@ -378,10 +378,10 @@ def test_get_worker_state(client: BlueapiClient):
378378
def test_set_state_transition_error(client: BlueapiClient):
379379
with pytest.raises(BlueskyRemoteControlError) as exception:
380380
client.resume()
381-
assert "<Response [400]>" in str(exception)
381+
assert exception.value.args[0]
382382
with pytest.raises(BlueskyRemoteControlError) as exception:
383383
client.pause()
384-
assert "<Response [400]>" in str(exception)
384+
assert exception.value.args[0]
385385

386386

387387
def test_get_task_by_status(rest_client: BlueapiRestClient):

tests/unit_tests/cli/test_cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ def test_authentication_error_caught_by_wrapper_func(
128128

129129
@patch("blueapi.client.rest.requests.Session.request")
130130
def test_remote_error_raised_by_wrapper_func(mock_requests: Mock, runner: CliRunner):
131-
mock_requests.side_effect = BlueskyRemoteControlError(message="Response [450]")
131+
mock_requests.side_effect = BlueskyRemoteControlError("Response [450]")
132132
result = runner.invoke(main, ["controller", "plans"])
133133
assert (
134134
isinstance(result.exception, BlueskyRemoteControlError)
135-
and result.exception.args[1] == "Response [450]"
135+
and result.exception.args == ("Response [450]",)
136136
and result.exit_code == 1
137137
)
138138

@@ -678,7 +678,7 @@ def test_env_reload_server_side_error(runner: CliRunner):
678678
assert isinstance(result.exception, BlueskyRemoteControlError), (
679679
"Expected a BlueskyRemoteError from cli runner"
680680
)
681-
assert result.exception.args[1] == "Failed to tear down the environment"
681+
assert result.exception.args[0] == "Failed to tear down the environment"
682682

683683
# Check if the endpoints were hit as expected
684684
assert len(responses.calls) == 1 # +1 for the DELETE call
@@ -714,7 +714,7 @@ def test_env_reload_server_side_error(runner: CliRunner):
714714
"Error: Incorrect parameters supplied\n Missing value for 'foo'\n",
715715
),
716716
(
717-
BlueskyRemoteControlError(message="Server error"),
717+
BlueskyRemoteControlError("Server error"),
718718
"Error: remote control error: Server error\n",
719719
),
720720
(

tests/unit_tests/client/test_client.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def test_create_and_start_task_fails_if_task_creation_fails(
225225
client: BlueapiClient,
226226
mock_rest: Mock,
227227
):
228-
mock_rest.create_task.side_effect = BlueskyRemoteControlError(message="No can do")
228+
mock_rest.create_task.side_effect = BlueskyRemoteControlError("No can do")
229229
with pytest.raises(BlueskyRemoteControlError):
230230
client.create_and_start_task(
231231
TaskRequest(name="baz", instrument_session="cm12345-1")
@@ -249,9 +249,7 @@ def test_create_and_start_task_fails_if_task_start_fails(
249249
mock_rest: Mock,
250250
):
251251
mock_rest.create_task.return_value = TaskResponse(task_id="baz")
252-
mock_rest.update_worker_task.side_effect = BlueskyRemoteControlError(
253-
message="No can do"
254-
)
252+
mock_rest.update_worker_task.side_effect = BlueskyRemoteControlError("No can do")
255253
with pytest.raises(BlueskyRemoteControlError):
256254
client.create_and_start_task(
257255
TaskRequest(name="baz", instrument_session="cm12345-1")

0 commit comments

Comments
 (0)