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

APIForbidden should result in 403 status #4943

Merged
merged 1 commit into from
Mar 4, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions supervisor/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,15 @@ def api_return_error(
JSON_RESULT: RESULT_ERROR,
JSON_MESSAGE: message or "Unknown error, see supervisor",
}
if isinstance(error, APIError) and error.job_id:
result[JSON_JOB_ID] = error.job_id
status = 400
if isinstance(error, APIError):
status = error.status
if error.job_id:
result[JSON_JOB_ID] = error.job_id

return web.json_response(
result,
status=400,
status=status,
dumps=json_dumps,
)

Expand Down
4 changes: 4 additions & 0 deletions supervisor/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ class HostLogError(HostError):
class APIError(HassioError, RuntimeError):
"""API errors."""

status = 400

def __init__(
self,
message: str | None = None,
Expand All @@ -322,6 +324,8 @@ def __init__(
class APIForbidden(APIError):
"""API forbidden error."""

status = 403


class APIAddonNotInstalled(APIError):
"""Not installed addon requested at addons API."""
Expand Down
2 changes: 1 addition & 1 deletion tests/api/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def test_api_discovery_forbidden(
with caplog.at_level(logging.ERROR):
resp = await api_client.post("/discovery", json={"service": "mqtt"})

assert resp.status == 400
assert resp.status == 403
result = await resp.json()
assert result["result"] == "error"
assert (
Expand Down
Loading