From cac8bae3dcd4ed66267afebade3eade6b1268d58 Mon Sep 17 00:00:00 2001 From: Alan Peixinho Date: Thu, 3 Dec 2026 15:48:35 -0300 Subject: [PATCH] fix(dashboard): accept a 404 that carries an error body A missing resource will come back as 404 with the same error field the API returns as 200 today. Read that body instead of aborting. A 404 without the field still fails the request. Assisted-by: Cursor Grok 4.7 Signed-off-by: Alan Peixinho --- kcidev/libs/dashboard.py | 13 ++++++++++--- tests/test_dashboard.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/kcidev/libs/dashboard.py b/kcidev/libs/dashboard.py index 38c8472..4250dfb 100644 --- a/kcidev/libs/dashboard.py +++ b/kcidev/libs/dashboard.py @@ -107,9 +107,16 @@ def wrapper( kci_err(f"Failed after {max_retries} retries with 500 error.") raise click.Abort() - r.raise_for_status() - - data = r.json() + # 404 + {"error": ...} is a missing resource, same as today's 200. + data = None + if r.status_code == 404: + try: + data = r.json() + except ValueError: + pass + if not isinstance(data, dict) or "error" not in data: + r.raise_for_status() + data = r.json() logging.debug(f"Response data size: {len(json.dumps(data))} bytes") if not isinstance(data, (dict, list)): diff --git a/tests/test_dashboard.py b/tests/test_dashboard.py index e19dd1a..0259bd1 100644 --- a/tests/test_dashboard.py +++ b/tests/test_dashboard.py @@ -4,6 +4,7 @@ import click import pytest +import requests from click.testing import CliRunner from kcidev.libs import dashboard @@ -316,6 +317,39 @@ def test_other_dashboard_errors_are_passed_through_unchanged(monkeypatch): assert excinfo.value.message == "Build not found" +def test_not_found_with_error_body_uses_the_same_error(monkeypatch): + response = Mock(status_code=404) + response.json.return_value = {"error": "Build not found", "resource": "build"} + response.raise_for_status.side_effect = requests.HTTPError("404") + monkeypatch.setattr(dashboard.kcidev_session, "get", Mock(return_value=response)) + + with pytest.raises(click.ClickException) as excinfo: + dashboard.dashboard_api_fetch("build/x", {}, False) + + assert excinfo.value.message == "Build not found" + + +@pytest.mark.parametrize( + "status, body", + [ + (404, ValueError("not json")), + (404, {"detail": "Not found."}), + (400, {"error": "Invalid parameter"}), + ], +) +def test_other_failures_still_abort(monkeypatch, status, body): + response = Mock(status_code=status) + if isinstance(body, Exception): + response.json.side_effect = body + else: + response.json.return_value = body + response.raise_for_status.side_effect = requests.HTTPError(str(status)) + monkeypatch.setattr(dashboard.kcidev_session, "get", Mock(return_value=response)) + + with pytest.raises(click.Abort): + dashboard.dashboard_api_fetch("no/such/route", {}, False) + + def _issue_collection_get(monkeypatch): response = Mock(status_code=200) response.json.return_value = {"issues": [{"id": "maestro:one"}]}