Improve bad JSON data reporting (#47932)
* Improve bad data reporting * Fix tests Co-authored-by: Erik <erik@montnemery.com>
This commit is contained in:
parent
9ec4c07753
commit
7fe3c472e9
3 changed files with 28 additions and 10 deletions
|
@ -112,12 +112,15 @@ def find_paths_unserializable_data(
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# We convert states and events to dict so we can find bad data inside it
|
# We convert objects with as_dict to their dict values so we can find bad data inside it
|
||||||
|
if hasattr(obj, "as_dict"):
|
||||||
|
desc = obj.__class__.__name__
|
||||||
if isinstance(obj, State):
|
if isinstance(obj, State):
|
||||||
obj_path += f"(state: {obj.entity_id})"
|
desc += f": {obj.entity_id}"
|
||||||
obj = obj.as_dict()
|
|
||||||
elif isinstance(obj, Event):
|
elif isinstance(obj, Event):
|
||||||
obj_path += f"(event: {obj.event_type})"
|
desc += f": {obj.event_type}"
|
||||||
|
|
||||||
|
obj_path += f"({desc})"
|
||||||
obj = obj.as_dict()
|
obj = obj.as_dict()
|
||||||
|
|
||||||
if isinstance(obj, dict):
|
if isinstance(obj, dict):
|
||||||
|
|
|
@ -67,7 +67,7 @@ async def test_pending_msg_peak(hass, mock_low_peak, hass_ws_client, caplog):
|
||||||
|
|
||||||
|
|
||||||
async def test_non_json_message(hass, websocket_client, caplog):
|
async def test_non_json_message(hass, websocket_client, caplog):
|
||||||
"""Test trying to serialze non JSON objects."""
|
"""Test trying to serialize non JSON objects."""
|
||||||
bad_data = object()
|
bad_data = object()
|
||||||
hass.states.async_set("test_domain.entity", "testing", {"bad": bad_data})
|
hass.states.async_set("test_domain.entity", "testing", {"bad": bad_data})
|
||||||
await websocket_client.send_json({"id": 5, "type": "get_states"})
|
await websocket_client.send_json({"id": 5, "type": "get_states"})
|
||||||
|
@ -77,6 +77,6 @@ async def test_non_json_message(hass, websocket_client, caplog):
|
||||||
assert msg["type"] == const.TYPE_RESULT
|
assert msg["type"] == const.TYPE_RESULT
|
||||||
assert not msg["success"]
|
assert not msg["success"]
|
||||||
assert (
|
assert (
|
||||||
f"Unable to serialize to JSON. Bad data found at $.result[0](state: test_domain.entity).attributes.bad={bad_data}(<class 'object'>"
|
f"Unable to serialize to JSON. Bad data found at $.result[0](State: test_domain.entity).attributes.bad={bad_data}(<class 'object'>"
|
||||||
in caplog.text
|
in caplog.text
|
||||||
)
|
)
|
||||||
|
|
|
@ -153,7 +153,7 @@ def test_find_unserializable_data():
|
||||||
[State("mock_domain.mock_entity", "on", {"bad": bad_data})],
|
[State("mock_domain.mock_entity", "on", {"bad": bad_data})],
|
||||||
dump=partial(dumps, cls=MockJSONEncoder),
|
dump=partial(dumps, cls=MockJSONEncoder),
|
||||||
)
|
)
|
||||||
== {"$[0](state: mock_domain.mock_entity).attributes.bad": bad_data}
|
== {"$[0](State: mock_domain.mock_entity).attributes.bad": bad_data}
|
||||||
)
|
)
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
|
@ -161,5 +161,20 @@ def test_find_unserializable_data():
|
||||||
[Event("bad_event", {"bad_attribute": bad_data})],
|
[Event("bad_event", {"bad_attribute": bad_data})],
|
||||||
dump=partial(dumps, cls=MockJSONEncoder),
|
dump=partial(dumps, cls=MockJSONEncoder),
|
||||||
)
|
)
|
||||||
== {"$[0](event: bad_event).data.bad_attribute": bad_data}
|
== {"$[0](Event: bad_event).data.bad_attribute": bad_data}
|
||||||
|
)
|
||||||
|
|
||||||
|
class BadData:
|
||||||
|
def __init__(self):
|
||||||
|
self.bla = bad_data
|
||||||
|
|
||||||
|
def as_dict(self):
|
||||||
|
return {"bla": self.bla}
|
||||||
|
|
||||||
|
assert (
|
||||||
|
find_paths_unserializable_data(
|
||||||
|
BadData(),
|
||||||
|
dump=partial(dumps, cls=MockJSONEncoder),
|
||||||
|
)
|
||||||
|
== {"$(BadData).bla": bad_data}
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue