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

Abstract adding json respone to error message #1224

Merged
Merged
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
32 changes: 21 additions & 11 deletions nailgun/entity_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@
CREATE_MISSING = False


def raise_for_status_add_to_exception(response):
"""Add error message from response to exception.

:param response, a ``requests.response`` object.
:raises: ``requests.exceptions.HTTPError`` if the response has an HTTP 4XX or 5XX status code.
"""
try:
response.raise_for_status()
except HTTPError as e:
with contextlib.suppress(JSONDecodeError):
e.args += (response.json(),)
raise e


def call_entity_method_with_timeout(entity_callable, timeout=300, **kwargs):
"""Call Entity callable with a custom timeout.

Expand Down Expand Up @@ -112,7 +126,7 @@ def raise_task_timeout(): # pragma: no cover
timer.start()
while True:
response = client.get(path, **server_config.get_client_kwargs())
response.raise_for_status()
raise_for_status_add_to_exception(response)
task_info = response.json()
if task_info['state'] in ('paused', 'stopped'):
break
Expand Down Expand Up @@ -699,7 +713,7 @@ def delete(self, synchronous=True, timeout=None):

"""
response = self.delete_raw()
response.raise_for_status()
raise_for_status_add_to_exception(response)

if synchronous is True and response.status_code == http_client.ACCEPTED:
return _poll_task(response.json()['id'], self._server_config, timeout=timeout)
Expand Down Expand Up @@ -764,7 +778,7 @@ def read_json(self, params=None):

"""
response = self.read_raw(params=params)
response.raise_for_status()
raise_for_status_add_to_exception(response)
return response.json()

def read(self, entity=None, attrs=None, ignore=None, params=None):
Expand Down Expand Up @@ -951,12 +965,8 @@ def create_json(self, create_missing=None):

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

return response.json()

def create(self, create_missing=None):
Expand Down Expand Up @@ -1060,7 +1070,7 @@ def update_json(self, fields=None):

"""
response = self.update_raw(fields)
response.raise_for_status()
raise_for_status_add_to_exception(response)
return response.json()

def update(self, fields=None):
Expand Down Expand Up @@ -1237,7 +1247,7 @@ def search_json(self, fields=None, query=None):

"""
response = self.search_raw(fields, query)
response.raise_for_status()
raise_for_status_add_to_exception(response)
return response.json()

def search_normalize(self, results):
Expand Down
Loading