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
This commit is contained in:
Tobias Urdin 2020-04-08 11:25:11 +02:00
parent 11d2b72898
commit c024b4fc43
2 changed files with 9 additions and 1 deletions

View File

@ -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."""
try:
content_type = resp.headers.get("Content-Type", "")
except AttributeError:
content_type = ""
if content_type.startswith("application/json"):
try:
body_json = resp.json()

View File

@ -0,0 +1,5 @@
---
fixes:
- |
[`bug 1870264 <https://bugs.launchpad.net/python-magnumclient/+bug/1870264>`_]
Fixed bug where the error message from the response could not be parsed.