From f988eb6d89efd772af77ed0bbc41fde9764d640a Mon Sep 17 00:00:00 2001 From: Sam Betts Date: Fri, 11 May 2018 12:27:43 +0100 Subject: [PATCH] Stop double json decoding API error messages This patch adds support for the fixed error messages that aren't double JSON encoded. Change-Id: Ib39f65c89e3e96efddd9fa3b648145ae3d6159d3 Story: #1662228 Task: #19644 --- ironicclient/common/http.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/ironicclient/common/http.py b/ironicclient/common/http.py index 1d293a202..db3c06a8a 100644 --- a/ironicclient/common/http.py +++ b/ironicclient/common/http.py @@ -69,16 +69,24 @@ def _trim_endpoint_api_version(url): def _extract_error_json(body): """Return error_message from the HTTP response body.""" - error_json = {} try: body_json = jsonutils.loads(body) - if 'error_message' in body_json: - raw_msg = body_json['error_message'] - error_json = jsonutils.loads(raw_msg) except ValueError: - pass + return {} - return error_json + if 'error_message' not in body_json: + return {} + + try: + error_json = jsonutils.loads(body_json['error_message']) + except ValueError: + return body_json + + err_msg = (error_json.get('faultstring') or error_json.get('description')) + if err_msg: + body_json['error_message'] = err_msg + + return body_json def get_server(endpoint): @@ -433,12 +441,8 @@ class HTTPClient(VersionNegotiationMixin): if resp.status_code >= http_client.BAD_REQUEST: error_json = _extract_error_json(body_str) - # NOTE(vdrok): exceptions from ironic controllers' _lookup methods - # are constructed directly by pecan instead of wsme, and contain - # only description field raise exc.from_response( - resp, (error_json.get('faultstring') or - error_json.get('description')), + resp, error_json.get('error_message'), error_json.get('debuginfo'), method, url) elif resp.status_code in (http_client.MOVED_PERMANENTLY, http_client.FOUND, @@ -615,11 +619,7 @@ class SessionClient(VersionNegotiationMixin, adapter.LegacyJsonAdapter): return self._http_request(url, method, **kwargs) if resp.status_code >= http_client.BAD_REQUEST: error_json = _extract_error_json(resp.content) - # NOTE(vdrok): exceptions from ironic controllers' _lookup methods - # are constructed directly by pecan instead of wsme, and contain - # only description field - raise exc.from_response(resp, (error_json.get('faultstring') or - error_json.get('description')), + raise exc.from_response(resp, error_json.get('error_message'), error_json.get('debuginfo'), method, url) elif resp.status_code in (http_client.MOVED_PERMANENTLY, http_client.FOUND, http_client.USE_PROXY):