From 8ed387bcdc576379521b5e27dba123ab3c80476a Mon Sep 17 00:00:00 2001 From: dosas Date: Mon, 2 Sep 2024 12:06:15 +0200 Subject: [PATCH] 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 95b3ce10125cfbc8d9a6d8a8e2b38a5007dd1b16 Adds existing solution to all raise_for_status calls. (cherry picked from commit 08bbd9a113c9fee7905eb508eae0c73c471a8074) --- nailgun/entity_mixins.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/nailgun/entity_mixins.py b/nailgun/entity_mixins.py index e00cae9c..4e50e9c4 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):