Skip to content

BlueapiClient gives unhelpful error message when invalid JSON is returned #1472

@oliwenmandiamond

Description

@oliwenmandiamond

When trying to use BlueapiClient, I got the following error message when trying to run the below:

from blueapi.client import BlueapiClient

bc = BlueapiClient.from_config_file("path/to/config.yaml")
devs = bc.devices
plans = bc.plans
python -i /home/xol73553/Documents/BLUESKY/i09_blueapi.py 
Traceback (most recent call last):
  File "/scratch/bluesky_development/blueapi/venv/lib64/python3.12/site-packages/requests/models.py", line 976, in json
    return complexjson.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/json/decoder.py", line 338, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/json/decoder.py", line 356, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/xol73553/Documents/BLUESKY/i09_blueapi.py", line 22, in <module>
    devs = bc.devices
           ^^^^^^^^^^
  File "/usr/lib64/python3.12/functools.py", line 998, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/scratch/bluesky_development/blueapi/venv/lib64/python3.12/site-packages/observability_utils/tracing/decorators.py", line 151, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/scratch/bluesky_development/blueapi/src/blueapi/client/client.py", line 307, in devices
    return DeviceCache(self._rest)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/scratch/bluesky_development/blueapi/src/blueapi/client/client.py", line 111, in __init__
    for model in rest.get_devices().devices
                 ^^^^^^^^^^^^^^^^^^
  File "/scratch/bluesky_development/blueapi/src/blueapi/client/rest.py", line 162, in get_devices
    return self._request_and_deserialize("/devices", DeviceResponse)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/scratch/bluesky_development/blueapi/venv/lib64/python3.12/site-packages/observability_utils/tracing/decorators.py", line 151, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/scratch/bluesky_development/blueapi/src/blueapi/client/rest.py", line 290, in _request_and_deserialize
    deserialized = TypeAdapter(target_type).validate_python(response.json())
                                                            ^^^^^^^^^^^^^^^
  File "/scratch/bluesky_development/blueapi/venv/lib64/python3.12/site-packages/requests/models.py", line 980, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The response for devices contains no json, so it provides a JSONDecodeError. The reason the response provides no valid json was because I was not logged in any more.

>>> bc.login()
Logging in
Please login from this URL:- https://identity.diamond.ac.uk/realms/dls/device?user_code=ULED-SLXT
Logged in and cached new token
>>> exit()

Now rerunning the script

python -i /home/xol73553/Documents/BLUESKY/i09_blueapi.py 
>>> 

It works! But the error message above didn't indicate the issue is due to not being logged in.

Acceptance critera

The client should instead make a check to see if the user is logged in first and then provide a useful error message to the user saying unable to retrieve plans / devices, please login first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: clientRelates to client codedeploymentStuff related to running in production containeronboardingGood for newcomers

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions