Skip to content

Commit 7be9776

Browse files
committed
🐛 Improve error message when receiving a 403
1 parent 7bef357 commit 7be9776

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

src/fastapi_cloud_cli/utils/api.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,18 @@ def _handle_unauthorized(auth_mode: AuthMode) -> str:
209209
return message
210210

211211

212+
def _get_response_error_message(response: httpx.Response) -> str | None:
213+
try:
214+
data = response.json()
215+
except json.JSONDecodeError:
216+
return None
217+
218+
try:
219+
return data["detail"]
220+
except (KeyError, TypeError):
221+
return None
222+
223+
212224
def handle_http_error(
213225
error: httpx.HTTPError,
214226
default_message: str | None = None,
@@ -227,7 +239,10 @@ def handle_http_error(
227239
message = _handle_unauthorized(auth_mode=auth_mode)
228240

229241
elif status_code == 403:
230-
message = "You don't have permissions for this resource"
242+
message = (
243+
_get_response_error_message(error.response)
244+
or "You don't have permissions for this resource"
245+
)
231246

232247
if not message:
233248
message = (

tests/test_cli_deploy.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,38 @@ def test_creates_app_on_backend(
461461
assert "App created successfully" in result.output
462462

463463

464+
@pytest.mark.respx
465+
def test_shows_api_message_when_create_app_is_forbidden(
466+
logged_in_cli: None, tmp_path: Path, respx_mock: respx.MockRouter
467+
) -> None:
468+
steps = [Keys.ENTER, Keys.ENTER, *"demo", Keys.ENTER, Keys.ENTER, Keys.ENTER]
469+
team = _get_random_team()
470+
471+
respx_mock.get("/teams/").mock(
472+
return_value=Response(200, json={"data": [team]})
473+
)
474+
respx_mock.post(
475+
"/apps/", json={"name": "demo", "team_id": team["id"], "directory": None}
476+
).mock(
477+
return_value=Response(
478+
403,
479+
json={"detail": "Your account cannot create apps for this team."},
480+
)
481+
)
482+
483+
with (
484+
changing_dir(tmp_path),
485+
patch("rich_toolkit.container.getchar") as mock_getchar,
486+
):
487+
mock_getchar.side_effect = steps
488+
489+
result = runner.invoke(app, ["deploy"])
490+
491+
assert result.exit_code == 1
492+
assert "You don't have permissions for this resource" in result.output
493+
assert "API message: Your account cannot create apps for this team." in result.output
494+
495+
464496
@pytest.mark.respx
465497
def test_creates_app_with_directory(
466498
logged_in_cli: None, tmp_path: Path, respx_mock: respx.MockRouter

0 commit comments

Comments
 (0)