Skip to content

Commit

Permalink
APIForbidden should result in 403 status
Browse files Browse the repository at this point in the history
  • Loading branch information
mdegat01 committed Mar 4, 2024
1 parent 3a2c3e2 commit bdb1b21
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
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

0 comments on commit bdb1b21

Please sign in to comment.