Skip to content

Figure out how to handle sporadic non-JSON MQTT payloads #5

Description

@dlenski

After a few days to a few weeks of running liten-up, one of my Mysa V2 Lite devices will send a non-JSON payload, raising an exception at https://github.com/dlenski/mysotherm/blob/main/mysotherm/liten_up.py#L35

Traceback (most recent call last):
  File "/home/dlenski/mysotherm/mysotherm/liten_up.py", line 35, in translate_packet
    payload = json.loads(pkt.payload, object_hook=slurpy)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/__init__.py", line 359, in loads
    return cls(**kw).decode(s)
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 136 (char 135)

Given that the error occurs 100+ characters into the string, I'm guessing that it's only "slightly" non-conformant JSON. As of a609523 and 550f8ae, we use non-strict JSON parsing to accept embedded control characters like \n in strings, and those give a different error message anyway:

>>> try: json.loads('{"fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo": "foo\nbar"}')
... except ValueError as exc: print(repr(exc), vars(exc))
... 
JSONDecodeError('Invalid control character at: line 1 column 130 (char 129)') {'msg': 'Invalid control character at', 'doc': '{"fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo": "foo\nbar"}', 'pos': 129, 'lineno': 1, 'colno': 130}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions