gagebenne/pydexcom

JSONDecodeError, raised exception not handled in _handle_response

shoaib42 opened this issue · 0 comments

First off thanks for sharing this awesome package!
I've lately been getting these exceptions, and its been erratic and not easily reproducible. The way I've been using is

  • Poll every 320 seconds
  • If there is an raised exception, try reconnecting and fetching again.
  • If the reading is way older than 5 minutes try again after a brief sleep

From what I can deduce, the error happens when old reading is being sent as the last reading and right after there is some exception raised for which I handle and reconnect (spin up a new Dexcom object) and another exception is thrown.

I'm thinking if we have the following if statement handled slightly differently. Happy to contribute!

    def _handle_response(self, response: requests.Response) -> Optional[DexcomError]:
        error: Optional[DexcomError] = None
        """
        Parse `requests.Response` for `pydexcom.errors.DexcomError`.

        :param response: `requests.Response` to parse
        """
        if response.json(): # the json() method is raising the error

to

        try :
            if response.json():
               ### existing code
        except :
            error = ServerResponseError() # new subclass of DexcomError

The error logs that I have

wrapper.sh[6982]: 2024.03.15 18:55:18 __main__.INFO: at (2024-03-15 18:49:57) -> 5.2 mmol/l �<86><92>
wrapper.sh[6982]: 2024.03.15 18:56:20 __main__.INFO: at (2024-03-15 18:49:57) -> 5.2 mmol/l �<86><92>
wrapper.sh[6982]: 2024.03.15 18:57:23 __main__.INFO: at (2024-03-15 18:49:57) -> 5.2 mmol/l �<86><92>
wrapper.sh[6982]: 2024.03.15 18:58:25 __main__.INFO: at (2024-03-15 18:49:57) -> 5.2 mmol/l �<86><92>
wrapper.sh[6982]: Traceback (most recent call last):
wrapper.sh[6982]:   File "/home/t1duser/.venv/lib/python3.11/site-packages/requests/models.py", line 971, in json
wrapper.sh[6982]:     return complexjson.loads(self.text, **kwargs)
wrapper.sh[6982]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wrapper.sh[6982]:   File "/usr/lib/python3.11/json/__init__.py", line 346, in loads
wrapper.sh[6982]:     return _default_decoder.decode(s)
wrapper.sh[6982]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^
wrapper.sh[6982]:   File "/usr/lib/python3.11/json/decoder.py", line 337, in decode
wrapper.sh[6982]:     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
wrapper.sh[6982]:                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wrapper.sh[6982]:   File "/usr/lib/python3.11/json/decoder.py", line 355, in raw_decode
wrapper.sh[6982]:     raise JSONDecodeError("Expecting value", s, err.value) from None
wrapper.sh[6982]: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
wrapper.sh[6982]: During handling of the above exception, another exception occurred:
wrapper.sh[6982]: Traceback (most recent call last):
wrapper.sh[6982]:   File "/home/t1duser/dexcom_svc/test.py", line 319, in <module>
wrapper.sh[6982]:     main()
wrapper.sh[6982]:   File "/home/t1duser/dexcom_svc/test.py", line 315, in main
wrapper.sh[6982]:     connect(conf['username'], conf['password'], ous=conf['ous'])
wrapper.sh[6982]:   File "/home/t1duser/dexcom_svc/test.py", line 68, in connect
wrapper.sh[6982]:     DEXCOM = Dexcom(username, password, ous=ous)
wrapper.sh[6982]:              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wrapper.sh[6982]:   File "/home/t1duser/.venv/lib/python3.11/site-packages/pydexcom/__init__.py", line 129, in __init__
wrapper.sh[6982]:     self._session()
wrapper.sh[6982]:   File "/home/t1duser/.venv/lib/python3.11/site-packages/pydexcom/__init__.py", line 242, in _session
wrapper.sh[6982]:     self._account_id = self._get_account_id()
wrapper.sh[6982]:                        ^^^^^^^^^^^^^^^^^^^^^^
wrapper.sh[6982]:   File "/home/t1duser/.venv/lib/python3.11/site-packages/pydexcom/__init__.py", line 215, in _get_account_id
wrapper.sh[6982]:     return self._post(
wrapper.sh[6982]:            ^^^^^^^^^^^
wrapper.sh[6982]:   File "/home/t1duser/.venv/lib/python3.11/site-packages/pydexcom/__init__.py", line 154, in _post
wrapper.sh[6982]:     error = self._handle_response(response)
wrapper.sh[6982]:             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wrapper.sh[6982]:   File "/home/t1duser/.venv/lib/python3.11/site-packages/pydexcom/__init__.py", line 167, in _handle_response
wrapper.sh[6982]:     if response.json():
wrapper.sh[6982]:        ^^^^^^^^^^^^^^^
wrapper.sh[6982]:   File "/home/t1duser/.venv/lib/python3.11/site-packages/requests/models.py", line 975, in json
wrapper.sh[6982]:     raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
wrapper.sh[6982]: requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
systemd[1]: dexcom_svc.service: Main process exited, code=exited, status=1/FAILURE
systemd[1]: dexcom_svc.service: Failed with result 'exit-code'.
systemd[1]: dexcom_svc.service: Consumed 3.619s CPU time.