mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Improve error handling for WS API trace/get (#48502)
This commit is contained in:
parent
6fe04f40a2
commit
e2f8bce0a0
@ -61,7 +61,14 @@ def websocket_trace_get(hass, connection, msg):
|
|||||||
key = (msg["domain"], msg["item_id"])
|
key = (msg["domain"], msg["item_id"])
|
||||||
run_id = msg["run_id"]
|
run_id = msg["run_id"]
|
||||||
|
|
||||||
trace = hass.data[DATA_TRACE][key][run_id]
|
try:
|
||||||
|
trace = hass.data[DATA_TRACE][key][run_id]
|
||||||
|
except KeyError:
|
||||||
|
connection.send_error(
|
||||||
|
msg["id"], websocket_api.ERR_NOT_FOUND, "The trace could not be found"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
message = websocket_api.messages.result_message(msg["id"], trace)
|
message = websocket_api.messages.result_message(msg["id"], trace)
|
||||||
|
|
||||||
connection.send_message(json.dumps(message, cls=TraceJSONEncoder, allow_nan=False))
|
connection.send_message(json.dumps(message, cls=TraceJSONEncoder, allow_nan=False))
|
||||||
|
@ -281,6 +281,25 @@ async def test_get_trace(hass, hass_ws_client, domain, prefix):
|
|||||||
assert response["result"] == contexts
|
assert response["result"] == contexts
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("domain", ["automation", "script"])
|
||||||
|
async def test_get_invalid_trace(hass, hass_ws_client, domain):
|
||||||
|
"""Test getting a non-existing trace."""
|
||||||
|
assert await async_setup_component(hass, domain, {domain: {}})
|
||||||
|
client = await hass_ws_client()
|
||||||
|
await client.send_json(
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"type": "trace/get",
|
||||||
|
"domain": domain,
|
||||||
|
"item_id": "sun",
|
||||||
|
"run_id": "invalid",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
response = await client.receive_json()
|
||||||
|
assert not response["success"]
|
||||||
|
assert response["error"]["code"] == "not_found"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("domain", ["automation", "script"])
|
@pytest.mark.parametrize("domain", ["automation", "script"])
|
||||||
async def test_trace_overflow(hass, hass_ws_client, domain):
|
async def test_trace_overflow(hass, hass_ws_client, domain):
|
||||||
"""Test the number of stored traces per script or automation is limited."""
|
"""Test the number of stored traces per script or automation is limited."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user