From c024b4fc431cbbd65ccb5b901dddb5e540e7ff41 Mon Sep 17 00:00:00 2001 From: Tobias Urdin Date: Wed, 8 Apr 2020 11:25:11 +0200 Subject: [PATCH] Check response type in _extract_error_json The response being passed to _extract_error_json is assumed to be a requests.Response but can also be a http.client.HTTPResponse. This patch adds a try statement where it first tries for a requests.Response and if not set falls back on parsing the string body instead of trying to use the json() helper function in requests.Response. Change-Id: I61840d284b5d3a249f621822b277c9bd6825140d Closes-Bug: 1870264 --- magnumclient/common/httpclient.py | 5 ++++- releasenotes/notes/bug-1870264-1b4f14618b8422f9.yaml | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/bug-1870264-1b4f14618b8422f9.yaml diff --git a/magnumclient/common/httpclient.py b/magnumclient/common/httpclient.py index 038fb21a..a68bffef 100644 --- a/magnumclient/common/httpclient.py +++ b/magnumclient/common/httpclient.py @@ -60,7 +60,10 @@ def _extract_error_json_text(body_json): def _extract_error_json(body, resp): """Return error_message from the HTTP response body.""" - content_type = resp.headers.get("Content-Type", "") + try: + content_type = resp.headers.get("Content-Type", "") + except AttributeError: + content_type = "" if content_type.startswith("application/json"): try: body_json = resp.json() diff --git a/releasenotes/notes/bug-1870264-1b4f14618b8422f9.yaml b/releasenotes/notes/bug-1870264-1b4f14618b8422f9.yaml new file mode 100644 index 00000000..7cdab0b5 --- /dev/null +++ b/releasenotes/notes/bug-1870264-1b4f14618b8422f9.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + [`bug 1870264 `_] + Fixed bug where the error message from the response could not be parsed.