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

Extend error message #10

Merged
merged 3 commits into from
Jul 16, 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
15 changes: 13 additions & 2 deletions src/npg_porch_cli/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def update_task(action: PorchAction, pipeline: Pipeline):
raise TypeError(f"task_status cannot be None for action '{action.action}'")
return send_request(
validate_ca_cert=action.validate_ca_cert,
url=urljoin(action.porch_url, "tasks"),
url=urljoin(action.porch_url, "tasks/"),
method="PUT",
data={
"pipeline": asdict(pipeline),
Expand Down Expand Up @@ -319,9 +319,20 @@ def send_request(

response = requests.request(method, url, **request_args)
if not response.ok:
raise ServerErrorException(
detail = ""
try:
data = response.json()
if "detail" in data:
detail = data["detail"]
except Exception:
pass

message = (
f'Status code {response.status_code} "{response.reason}" '
f"received from {response.url}"
)
if detail:
message += f".\nDetail: {detail}"
raise ServerErrorException(message)

return response.json()
27 changes: 25 additions & 2 deletions tests/test_send_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ def __init__(self):
self.ok = False

def json(self):
return {"Error": "Not found"}
return {"detail": "Not found in our data"}


class MockResponseNotFoundShort:
def __init__(self):
self.status_code = 404
self.reason = "NOT FOUND"
self.url = url
self.ok = False

def json(self):
return {}


def mock_get_200(*args, **kwargs):
Expand All @@ -39,8 +50,11 @@ def mock_get_404(*args, **kwargs):
return MockResponseNotFound()


def test_sending_request(monkeypatch):
def mock_get_404_short(*args, **kwargs):
return MockResponseNotFoundShort()


def test_sending_request(monkeypatch):
monkeypatch.delenv(var_name, raising=False)

with pytest.raises(ValueError) as e:
Expand All @@ -66,6 +80,15 @@ def test_sending_request(monkeypatch):

with monkeypatch.context() as m:
m.setattr(requests, "request", mock_get_404)
with pytest.raises(ServerErrorException) as e:
send_request(validate_ca_cert=False, url=url, method="POST", data=json_data)
assert e.value.args[0] == (
'Status code 404 "NOT FOUND" received from '
f"{url}.\nDetail: Not found in our data"
)

with monkeypatch.context() as m:
m.setattr(requests, "request", mock_get_404_short)
with pytest.raises(ServerErrorException) as e:
send_request(validate_ca_cert=False, url=url, method="POST", data=json_data)
assert e.value.args[0] == f'Status code 404 "NOT FOUND" received from {url}'
Expand Down