Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign specific error code for HomeAssistantError on websocket_api connection exceptions #104700

Merged
merged 1 commit into from Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/websocket_api/connection.py
Expand Up @@ -271,7 +271,7 @@ def async_handle_exception(self, msg: dict[str, Any], err: Exception) -> None:
err_message = "Timeout"
elif isinstance(err, HomeAssistantError):
err_message = str(err)
code = const.ERR_UNKNOWN_ERROR
code = const.ERR_HOME_ASSISTANT_ERROR
translation_domain = err.translation_domain
translation_key = err.translation_key
translation_placeholders = err.translation_placeholders
Expand Down
2 changes: 1 addition & 1 deletion tests/components/application_credentials/test_init.py
Expand Up @@ -479,7 +479,7 @@ async def test_config_flow(
resp = await client.cmd("delete", {"application_credentials_id": ID})
assert not resp.get("success")
assert "error" in resp
assert resp["error"].get("code") == "unknown_error"
assert resp["error"].get("code") == "home_assistant_error"
assert (
resp["error"].get("message")
== "Cannot delete credential in use by integration fake_integration"
Expand Down
4 changes: 2 additions & 2 deletions tests/components/blueprint/test_websocket_api.py
Expand Up @@ -438,7 +438,7 @@ async def test_delete_blueprint_in_use_by_automation(
assert msg["id"] == 9
assert not msg["success"]
assert msg["error"] == {
"code": "unknown_error",
"code": "home_assistant_error",
"message": "Blueprint in use",
}

Expand Down Expand Up @@ -484,6 +484,6 @@ async def test_delete_blueprint_in_use_by_script(
assert msg["id"] == 9
assert not msg["success"]
assert msg["error"] == {
"code": "unknown_error",
"code": "home_assistant_error",
"message": "Blueprint in use",
}
12 changes: 6 additions & 6 deletions tests/components/config/test_device_registry.py
Expand Up @@ -242,7 +242,7 @@ async def async_remove_config_entry_device(hass, config_entry, device_entry):
response = await ws_client.receive_json()

assert not response["success"]
assert response["error"]["code"] == "unknown_error"
assert response["error"]["code"] == "home_assistant_error"

# Make async_remove_config_entry_device return True
can_remove = True
Expand Down Expand Up @@ -365,7 +365,7 @@ async def async_remove_config_entry_device(hass, config_entry, device_entry):
response = await ws_client.receive_json()

assert not response["success"]
assert response["error"]["code"] == "unknown_error"
assert response["error"]["code"] == "home_assistant_error"
assert response["error"]["message"] == "Unknown config entry"

# Try removing a config entry which does not support removal from the device
Expand All @@ -380,7 +380,7 @@ async def async_remove_config_entry_device(hass, config_entry, device_entry):
response = await ws_client.receive_json()

assert not response["success"]
assert response["error"]["code"] == "unknown_error"
assert response["error"]["code"] == "home_assistant_error"
assert (
response["error"]["message"] == "Config entry does not support device removal"
)
Expand All @@ -397,7 +397,7 @@ async def async_remove_config_entry_device(hass, config_entry, device_entry):
response = await ws_client.receive_json()

assert not response["success"]
assert response["error"]["code"] == "unknown_error"
assert response["error"]["code"] == "home_assistant_error"
assert response["error"]["message"] == "Unknown device"

# Try removing a config entry from a device which it's not connected to
Expand Down Expand Up @@ -428,7 +428,7 @@ async def async_remove_config_entry_device(hass, config_entry, device_entry):
response = await ws_client.receive_json()

assert not response["success"]
assert response["error"]["code"] == "unknown_error"
assert response["error"]["code"] == "home_assistant_error"
assert response["error"]["message"] == "Config entry not in device"

# Try removing a config entry which can't be loaded from a device - allowed
Expand All @@ -443,5 +443,5 @@ async def async_remove_config_entry_device(hass, config_entry, device_entry):
response = await ws_client.receive_json()

assert not response["success"]
assert response["error"]["code"] == "unknown_error"
assert response["error"]["code"] == "home_assistant_error"
assert response["error"]["message"] == "Integration not found"
2 changes: 1 addition & 1 deletion tests/components/fritzbox/test_init.py
Expand Up @@ -296,7 +296,7 @@ async def test_remove_device(
)
response = await ws_client.receive_json()
assert not response["success"]
assert response["error"]["code"] == "unknown_error"
assert response["error"]["code"] == "home_assistant_error"
await hass.async_block_till_done()

# try to delete orphan_device
Expand Down
2 changes: 1 addition & 1 deletion tests/components/group/test_config_flow.py
Expand Up @@ -699,4 +699,4 @@ async def test_option_flow_sensor_preview_config_entry_removed(
)
msg = await client.receive_json()
assert not msg["success"]
assert msg["error"] == {"code": "unknown_error", "message": "Unknown error"}
assert msg["error"] == {"code": "home_assistant_error", "message": "Unknown error"}
4 changes: 2 additions & 2 deletions tests/components/input_select/test_init.py
Expand Up @@ -740,7 +740,7 @@ async def test_update_duplicates(
)
resp = await client.receive_json()
assert not resp["success"]
assert resp["error"]["code"] == "unknown_error"
assert resp["error"]["code"] == "home_assistant_error"
assert resp["error"]["message"] == "Duplicate options are not allowed"

state = hass.states.get(input_entity_id)
Expand Down Expand Up @@ -812,7 +812,7 @@ async def test_ws_create_duplicates(
)
resp = await client.receive_json()
assert not resp["success"]
assert resp["error"]["code"] == "unknown_error"
assert resp["error"]["code"] == "home_assistant_error"
assert resp["error"]["message"] == "Duplicate options are not allowed"

assert not hass.states.get(input_entity_id)
Expand Down
2 changes: 1 addition & 1 deletion tests/components/tag/test_init.py
Expand Up @@ -131,5 +131,5 @@ async def test_tag_id_exists(
await client.send_json({"id": 2, "type": f"{DOMAIN}/create", "tag_id": "test tag"})
response = await client.receive_json()
assert not response["success"]
assert response["error"]["code"] == "unknown_error"
assert response["error"]["code"] == "home_assistant_error"
assert len(changes) == 0
2 changes: 1 addition & 1 deletion tests/components/template/test_config_flow.py
Expand Up @@ -845,4 +845,4 @@ async def test_option_flow_sensor_preview_config_entry_removed(
)
msg = await client.receive_json()
assert not msg["success"]
assert msg["error"] == {"code": "unknown_error", "message": "Unknown error"}
assert msg["error"] == {"code": "home_assistant_error", "message": "Unknown error"}
2 changes: 1 addition & 1 deletion tests/components/websocket_api/test_commands.py
Expand Up @@ -2327,7 +2327,7 @@ async def test_execute_script(
translation_key="test_error",
translation_placeholders={"option": "bla"},
),
"unknown_error",
"home_assistant_error",
),
(
ServiceValidationError(
Expand Down
8 changes: 4 additions & 4 deletions tests/components/websocket_api/test_connection.py
Expand Up @@ -39,15 +39,15 @@
),
(
exceptions.HomeAssistantError("Failed to do X"),
websocket_api.ERR_UNKNOWN_ERROR,
websocket_api.ERR_HOME_ASSISTANT_ERROR,
"Failed to do X",
"Error handling message: Failed to do X (unknown_error) Mock User from 127.0.0.42 (Browser)",
"Error handling message: Failed to do X (home_assistant_error) Mock User from 127.0.0.42 (Browser)",
),
(
exceptions.ServiceValidationError("Failed to do X"),
websocket_api.ERR_UNKNOWN_ERROR,
websocket_api.ERR_HOME_ASSISTANT_ERROR,
"Failed to do X",
"Error handling message: Failed to do X (unknown_error) Mock User from 127.0.0.42 (Browser)",
"Error handling message: Failed to do X (home_assistant_error) Mock User from 127.0.0.42 (Browser)",
),
(
ValueError("Really bad"),
Expand Down