Skip to content

Commit 2c04805

Browse files
dosasweb-flow
authored andcommitted
Abstract adding json respone to error message
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)
1 parent d814337 commit 2c04805

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

nailgun/entity_mixins.py

+21-11
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@
4444
CREATE_MISSING = False
4545

4646

47+
def raise_for_status_add_to_exception(response):
48+
"""Add error message from response to exception.
49+
50+
:param response, a ``requests.response`` object.
51+
:raises: ``requests.exceptions.HTTPError`` if the response has an HTTP 4XX or 5XX status code.
52+
"""
53+
try:
54+
response.raise_for_status()
55+
except HTTPError as e:
56+
with contextlib.suppress(JSONDecodeError):
57+
e.args += (response.json(),)
58+
raise e
59+
60+
4761
def call_entity_method_with_timeout(entity_callable, timeout=300, **kwargs):
4862
"""Call Entity callable with a custom timeout.
4963
@@ -112,7 +126,7 @@ def raise_task_timeout(): # pragma: no cover
112126
timer.start()
113127
while True:
114128
response = client.get(path, **server_config.get_client_kwargs())
115-
response.raise_for_status()
129+
raise_for_status_add_to_exception(response)
116130
task_info = response.json()
117131
if task_info['state'] in ('paused', 'stopped'):
118132
break
@@ -699,7 +713,7 @@ def delete(self, synchronous=True, timeout=None):
699713
700714
"""
701715
response = self.delete_raw()
702-
response.raise_for_status()
716+
raise_for_status_add_to_exception(response)
703717

704718
if synchronous is True and response.status_code == http_client.ACCEPTED:
705719
return _poll_task(response.json()['id'], self._server_config, timeout=timeout)
@@ -764,7 +778,7 @@ def read_json(self, params=None):
764778
765779
"""
766780
response = self.read_raw(params=params)
767-
response.raise_for_status()
781+
raise_for_status_add_to_exception(response)
768782
return response.json()
769783

770784
def read(self, entity=None, attrs=None, ignore=None, params=None):
@@ -951,12 +965,8 @@ def create_json(self, create_missing=None):
951965
952966
"""
953967
response = self.create_raw(create_missing)
954-
try:
955-
response.raise_for_status()
956-
except HTTPError as e:
957-
with contextlib.suppress(JSONDecodeError):
958-
e.args += (response.json(),)
959-
raise e
968+
raise_for_status_add_to_exception(response)
969+
960970
return response.json()
961971

962972
def create(self, create_missing=None):
@@ -1060,7 +1070,7 @@ def update_json(self, fields=None):
10601070
10611071
"""
10621072
response = self.update_raw(fields)
1063-
response.raise_for_status()
1073+
raise_for_status_add_to_exception(response)
10641074
return response.json()
10651075

10661076
def update(self, fields=None):
@@ -1237,7 +1247,7 @@ def search_json(self, fields=None, query=None):
12371247
12381248
"""
12391249
response = self.search_raw(fields, query)
1240-
response.raise_for_status()
1250+
raise_for_status_add_to_exception(response)
12411251
return response.json()
12421252

12431253
def search_normalize(self, results):

0 commit comments

Comments
 (0)