diff --git a/nailgun/entity_mixins.py b/nailgun/entity_mixins.py index f7b3119d..91027fff 100644 --- a/nailgun/entity_mixins.py +++ b/nailgun/entity_mixins.py @@ -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. @@ -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 @@ -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) @@ -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): @@ -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): @@ -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): @@ -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):