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

Add json response to entity creation error message #1119

Merged
merged 1 commit into from
Apr 17, 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 nailgun/entity_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from fauxfactory import gen_choice
from inflection import pluralize
from requests.exceptions import HTTPError, JSONDecodeError

from nailgun import client, config
from nailgun.entity_fields import IntegerField, ListField, OneToManyField, OneToOneField
Expand Down Expand Up @@ -947,7 +948,12 @@ def create_json(self, create_missing=None):

"""
response = self.create_raw(create_missing)
response.raise_for_status()
try:
response.raise_for_status()
except HTTPError as e:
with contextlib.suppress(JSONDecodeError):
e.args += (response.json(),)
raise e
return response.json()

def create(self, create_missing=None):
Expand Down
23 changes: 22 additions & 1 deletion tests/test_entity_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest import TestCase, mock

from fauxfactory import gen_integer
from requests.exceptions import HTTPError
from requests.exceptions import HTTPError, JSONDecodeError

from nailgun import client, config, entity_mixins
from nailgun.entity_fields import (
Expand Down Expand Up @@ -621,6 +621,27 @@ def test_create_json(self):
self.assertEqual(response.raise_for_status.call_count, 1)
self.assertEqual(response.json.call_count, 1)

def test_create_json_with_exception(self):
"""Check what happens if the server returns an error HTTP status code for :meth:`nailgun.entity_mixins.EntityCreateMixin.create_json`."""
for valid_json in (True, False):
response = mock.Mock()
return_value = {"a": "b"}
response.raise_for_status.side_effect = HTTPError("foo")
if valid_json:
response.json.return_value = return_value
else:
response.json.side_effect = JSONDecodeError("msg", "foo", 2)
with mock.patch.object(
self.entity, 'create_raw', return_value=response
), self.assertRaises(HTTPError) as error:
self.entity.create_json()
self.assertEqual(response.raise_for_status.call_count, 1)
self.assertEqual(response.json.call_count, 1)
if valid_json:
self.assertEqual(error.exception.args[1], return_value)
else:
self.assertEqual(len(error.exception.args), 1)

def test_create(self):
"""Test :meth:`nailgun.entity_mixins.EntityCreateMixin.create`."""

Expand Down
Loading