Skip to content

Commit

Permalink
Abstract adding json respone to error message
Browse files Browse the repository at this point in the history
The output of raise_for_status is not really descriptive
for since the actual error message is not part of the
exception.

This commit exapnds on 95b3ce1

Adds existing solution to all raise_for_status calls.

(cherry picked from commit 08bbd9a)
  • Loading branch information
dosas authored and web-flow committed Oct 10, 2024
1 parent 41ecb58 commit ff1e9eb
Showing 1 changed file with 21 additions and 11 deletions.
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

0 comments on commit ff1e9eb

Please sign in to comment.