Repository navigation
Closed
Labels
Description
When the controller run subcommand is executed without providing any parameters (-p), the following is returned:
$ blueapi controller run scan
Traceback (most recent call last):
File "/scratch/30day_tmp/delete15may/bs/blueapi/venv/bin/blueapi", line 8, in <module>
sys.exit(main())
File "/scratch/30day_tmp/delete15may/bs/blueapi/venv/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
return self.main(*args, **kwargs)
File "/scratch/30day_tmp/delete15may/bs/blueapi/venv/lib/python3.10/site-packages/click/core.py", line 1055, in main
rv = self.invoke(ctx)
File "/scratch/30day_tmp/delete15may/bs/blueapi/venv/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/scratch/30day_tmp/delete15may/bs/blueapi/venv/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/scratch/30day_tmp/delete15may/bs/blueapi/venv/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/scratch/30day_tmp/delete15may/bs/blueapi/venv/lib/python3.10/site-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/scratch/30day_tmp/delete15may/bs/blueapi/src/blueapi/cli/cli.py", line 67, in wrapper
func(*args, **kwargs)
File "/scratch/30day_tmp/delete15may/bs/blueapi/venv/lib/python3.10/site-packages/click/decorators.py", line 38, in new_func
return f(get_current_context().obj, *args, **kwargs)
File "/scratch/30day_tmp/delete15may/bs/blueapi/src/blueapi/cli/cli.py", line 106, in run_plan
json=json.loads(parameters),
File "/dls_sw/apps/python/miniforge/4.10.0-0/envs/python3.10/lib/python3.10/json/__init__.py", line 339, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not NoneType
This is not particularly user friendly, if parameters are always required, can this be made into a mandatory argument?
Activity
[-]Unclear error message when running plan without data[/-][+]Unclear error message when running plan without parameters[/+]tpoliaw commentedon May 16, 2023
This was annoying me as well. Potentially fixed in #207
callumforrester commentedon Feb 7, 2024
This is now a 500 internal server error from a client perspective, which isn't much better
tpoliaw commentedon Feb 7, 2024
Has the server side handling of it changed? If parameters are needed it should be at least a 4xx error
callumforrester commentedon Feb 7, 2024
I believe so, and agreed, and also the CLI should translate HTTP codes into human-readable error messages
stan-dot commentedon Feb 16, 2024
debugging the error in parallel server and client side,
here the server

here the client

stan-dot commentedon Feb 16, 2024
at the moment the cli raises an error only in two cases:
yet the error text is about a
pickleoperation, so a different one from the May 2023 original post. I cannot be sure if this is an issue with setup on my end or just natural changing of the repotpoliaw commentedon Feb 16, 2024
The multiprocessing/pickling errors mean it's probably related to #343 if that helps narrow down why it's failing. If you do pass the correct parameters does it run as expected?
stan-dot commentedon Feb 28, 2024
reproducing the error documenting along:
name': 'scan', 'parameter_schema': {'additionalProperties': False, 'properties': {'axes_to_move': {'additionalProperties': {'_axes_to_move': '<class ' "'bluesky.protocols.Movable'>"}, 'title': 'Axes ' 'To ' 'Move', 'type': 'object'}, 'detectors': {'items': {'_detectors': '<class ' "'bluesky.protocols.Readable'>"}, 'title': 'Detectors', 'type': 'array'}, 'metadata': {'title': 'Metadata', 'type': 'object'}, 'spec': {'allOf': [{'type': 'string'}], 'title': 'Spec'}}, 'required': ['detectors', 'axes_to_move', 'spec'], 'title': 'scan', 'type': 'object'}},stan-dot commentedon Feb 28, 2024
are there plans that do not take any parameters?
parameters = parameters or "{}" task = RunPlan(name=name, params=json.loads(parameters))cli.pylines 184-5. A quickfix would be to reject-return here if no parameters are supplied.stan-dot commentedon Feb 28, 2024
also line 185 referenced above uses Task which is referenced in #253
tpoliaw commentedon Feb 29, 2024
There were when
parameterswas changed to be optional and it was annoying that they required passing-p '{}'. It may be that most plans need args now but presumably you can always write ones that don't?5 remaining items
stan-dot commentedon Mar 12, 2024
one possible refactor is changing the generic request to a named
requests.get/postcalls that can then be mocked more granularily. it's about this method inrest.pypython def _request_and_deserialize( self, suffix: str, target_type: Type[T], data: Optional[Mapping[str, Any]] = None, method="GET", raise_if: Callable[[requests.Response], bool] = _is_exception, ) -> T: url = self._url(suffix) # todo consider changing this to a switch statement for easier mocking # alternatively do single responsiblity principle # and decoule request from deserializing. response = requests.request(method, url, json=data) if raise_if(response): message = get_status_message(response.status_code) t = response.text error_message = f"""Response failed with text: {t}, with error code: {response.status_code} which corresponds to {message}""" raise BlueskyRemoteError(error_message) deserialized = parse_obj_as(target_type, response.json()) return deserializedstan-dot commentedon Mar 15, 2024
hacky solution the resembles the current tests
`mock_requests.return_value = client.get(f"/plan/{plan}")
bunch of testing
mock_requests.return_value = client.post("/tasks")
bunch more testing`
stan-dot commentedon Mar 18, 2024
this does not seem to work that well as the invocation of the
blueapai controller run sleep ...runs in the background and two different calls to the API from inside that line are hard to reconcile from the subsequent linesstan-dot commentedon Mar 27, 2024
merely trying to run
blueapi servewith the config returns an error:NFO: Application startup complete. ERROR: [Errno -2] Name or service not known INFO: Waiting for application shutdown.stan-dot commentedon Mar 27, 2024
still got a false negative erro in the test
stan-dot commentedon Mar 28, 2024
`from pydantic import BaseModel, ValidationError, validator
from typing import Optional
class TimeSchema(BaseModel):
time: float
`
result - runtime validation won't work just need to write a separate schema for each plan. which I fully support but that's another ticket. all plans and experiments could be standardized...
I mean we could add manual python validation against the 'properties' but that's not using pydantic as it was supposed to work
callumforrester commentedon Mar 28, 2024
Hmm, yeah, I think separate ticket for custom per-parameter validation
stan-dot commentedon Mar 28, 2024
how to mark this ticket then as pending and dependent on that ticket? I think gitlab has some better support for this kind of thing
stan-dot commentedon Mar 28, 2024
example for future reference: `import json
from pydantic import parse_obj_as
from typing import Any, Optional
from pydantic import BaseModel, Field
class MyBaseModel(BaseModel):
name = "base"
class MyPlanModel(MyBaseModel):
name: str = "Plan model"
description: Optional[str] = Field(
description="Docstring of the plan", default=None
)
parameter_schema: Optional[dict[str, Any]] = Field(
description="Schema of the plan's parameters",
alias="schema",
default_factory=dict,
)
plan_model: MyPlanModel = MyPlanModel()
plan_model.parameter_schema = {
"title": "sleep",
"type": "object",
"properties": {"time": {"title": "Time", "type": "number"}},
"required": ["time"],
"additionalProperties": False,
}
print(plan_model.dict())
wrong_params: str = '{"wrong":"aboslutely wrong"}'
p = json.loads(wrong_params) or "{}"
try:
validated = parse_obj_as(type(plan_model.parameter_schema), p)
print(validated)
except ValueError as e:
print(f"Error: {e}")
validated = None
`
callumforrester commentedon Apr 4, 2024
From discussion: Validation is already performed server-side and errors are shipped to the client here:
blueapi/src/blueapi/service/main.py
Line 152 in dba0f25
All the information needed should be in
resphere:blueapi/src/blueapi/cli/cli.py
Line 187 in dba0f25
stan-dot commentedon Apr 4, 2024
todo: