Skip to content
Open
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
13 changes: 10 additions & 3 deletions kcidev/libs/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down
34 changes: 34 additions & 0 deletions tests/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import click
import pytest
import requests
from click.testing import CliRunner

from kcidev.libs import dashboard
Expand Down Expand Up @@ -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"}]}
Expand Down
Loading