Include error message from server if one exists

Nova includes bodies in error responses that look like this:

  {
    "badRequest": {
      "message": "Invalid input for field/attribute fixed_address.",
      "code": 400
    }
  }

I recently had to look at HTTP debug logs to figure out a bug. Let's
include that data in our exceptions already.

Change-Id: I4b4b4702d772739b8f930ff6a3c1ad83987fed17
This commit is contained in:
Monty Taylor 2017-05-15 15:42:19 -05:00
parent 9ecf600397
commit dbb42fc6ff
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594

View File

@ -96,27 +96,33 @@ def raise_from_response(response, error_message=None):
elif 500 <= response.status_code < 600:
source = "Server"
else:
source = None
return
if response.reason:
remote_error = "Error: {reason} for {url}".format(
reason=response.reason,
url=response.url)
remote_error = "Error for url: {url}".format(url=response.url)
try:
details = response.json()
# Nova returns documents that look like
# {statusname: 'message': message, 'code': code}
if len(details.keys()) == 1:
detail_key = details.keys()[0]
detail_message = details[detail_key].get('message')
if detail_message:
remote_error += " {message}".format(message=detail_message)
except ValueError:
if response.reason:
remote_error += " {reason}".format(reason=response.reason)
if error_message:
msg = '{error_message}. ({code}) {source} {remote_error}'.format(
error_message=error_message,
source=source,
code=response.status_code,
remote_error=remote_error)
else:
remote_error = "Error for url: {url}".format(url=response.url)
if source:
if error_message:
msg = '{error_message}. ({code}) {source} {remote_error}'.format(
error_message=error_message,
source=source,
code=response.status_code,
remote_error=remote_error)
else:
msg = '({code}) {source} {remote_error}'.format(
code=response.status_code,
source=source,
remote_error=remote_error)
msg = '({code}) {source} {remote_error}'.format(
code=response.status_code,
source=source,
remote_error=remote_error)
# Special case 404 since we raised a specific one for neutron exceptions
# before