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

bugfix: fix error message printing error #576

Merged
merged 1 commit into from
Apr 4, 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
8 changes: 7 additions & 1 deletion pylxd/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
def __str__(self):
if self.response.status_code == 200: # Operation failure
try:
return self.response.json()["metadata"]["err"]
json_response = self.response.json()
metadata = json_response.get("metadata")
if metadata and isinstance(metadata, dict) and "err" in metadata:
return json_response["metadata"]["err"]

Check warning on line 23 in pylxd/exceptions.py

View check run for this annotation

Codecov / codecov/patch

pylxd/exceptions.py#L23

Added line #L23 was not covered by tests
if "error" in json_response:
return json_response["error"]
return str(json_response)

Check warning on line 26 in pylxd/exceptions.py

View check run for this annotation

Codecov / codecov/patch

pylxd/exceptions.py#L26

Added line #L26 was not covered by tests
except (ValueError, KeyError):
pass

Expand Down
23 changes: 23 additions & 0 deletions pylxd/models/tests/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ def error(request, context):

self.assertRaises(exceptions.LXDAPIException, an_operation.wait)

def test_wait_with_error_async_without_metadata(self):
"""If the operation errors, wait raises an exception."""

def error(request, context):
context.status_code = 200
return {"type": "async", "error": "Could not proceed", "metadata": None}

self.add_rule(
{
"json": error,
"method": "GET",
"url": r"^http://pylxd.test/1.0/operations/operation-abc/wait$",
}
)

name = "/1.0/operations/operation-abc"

an_operation = models.Operation.get(self.client, name)

with self.assertRaises(exceptions.LXDAPIException) as wait_cm:
an_operation.wait()
assert str(wait_cm.exception) == "Could not proceed"

def test_unknown_attribute(self):
self.add_rule(
{
Expand Down