|
44 | 44 | CREATE_MISSING = False
|
45 | 45 |
|
46 | 46 |
|
| 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 | + |
47 | 61 | def call_entity_method_with_timeout(entity_callable, timeout=300, **kwargs):
|
48 | 62 | """Call Entity callable with a custom timeout.
|
49 | 63 |
|
@@ -112,7 +126,7 @@ def raise_task_timeout(): # pragma: no cover
|
112 | 126 | timer.start()
|
113 | 127 | while True:
|
114 | 128 | response = client.get(path, **server_config.get_client_kwargs())
|
115 |
| - response.raise_for_status() |
| 129 | + raise_for_status_add_to_exception(response) |
116 | 130 | task_info = response.json()
|
117 | 131 | if task_info['state'] in ('paused', 'stopped'):
|
118 | 132 | break
|
@@ -699,7 +713,7 @@ def delete(self, synchronous=True, timeout=None):
|
699 | 713 |
|
700 | 714 | """
|
701 | 715 | response = self.delete_raw()
|
702 |
| - response.raise_for_status() |
| 716 | + raise_for_status_add_to_exception(response) |
703 | 717 |
|
704 | 718 | if synchronous is True and response.status_code == http_client.ACCEPTED:
|
705 | 719 | return _poll_task(response.json()['id'], self._server_config, timeout=timeout)
|
@@ -764,7 +778,7 @@ def read_json(self, params=None):
|
764 | 778 |
|
765 | 779 | """
|
766 | 780 | response = self.read_raw(params=params)
|
767 |
| - response.raise_for_status() |
| 781 | + raise_for_status_add_to_exception(response) |
768 | 782 | return response.json()
|
769 | 783 |
|
770 | 784 | def read(self, entity=None, attrs=None, ignore=None, params=None):
|
@@ -951,12 +965,8 @@ def create_json(self, create_missing=None):
|
951 | 965 |
|
952 | 966 | """
|
953 | 967 | 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 | + |
960 | 970 | return response.json()
|
961 | 971 |
|
962 | 972 | def create(self, create_missing=None):
|
@@ -1060,7 +1070,7 @@ def update_json(self, fields=None):
|
1060 | 1070 |
|
1061 | 1071 | """
|
1062 | 1072 | response = self.update_raw(fields)
|
1063 |
| - response.raise_for_status() |
| 1073 | + raise_for_status_add_to_exception(response) |
1064 | 1074 | return response.json()
|
1065 | 1075 |
|
1066 | 1076 | def update(self, fields=None):
|
@@ -1237,7 +1247,7 @@ def search_json(self, fields=None, query=None):
|
1237 | 1247 |
|
1238 | 1248 | """
|
1239 | 1249 | response = self.search_raw(fields, query)
|
1240 |
| - response.raise_for_status() |
| 1250 | + raise_for_status_add_to_exception(response) |
1241 | 1251 | return response.json()
|
1242 | 1252 |
|
1243 | 1253 | def search_normalize(self, results):
|
|
0 commit comments