Change nailgun error handling to json-compatible

Nailgun is wrapping HTTP errors to JSON as of
https://review.openstack.org/#/c/137642

Change-Id: Ic46a8b43f8d022dfbf26b5cc2ca64213b249c373
Depends-On: Ie351d6cd6a5ebf929563a2e0c76ef875fa71c2d3
Closes-Bug: #1425046
This commit is contained in:
Maciej Kwiek
2015-03-02 12:13:57 +01:00
parent 3ebfa9c14a
commit 533b453d20

View File

@@ -13,6 +13,7 @@
# under the License.
from functools import wraps
import json
from keystoneclient.exceptions import Unauthorized
import requests
import sys
@@ -95,8 +96,7 @@ def exceptions_decorator(func):
# when server returns to us bad request check that
# and print meaningful reason
except requests.HTTPError as exc:
error_body = exc.response.text
exit_with_error("{0} ({1})".format(exc, error_body))
exit_with_error("{0} ({1})".format(exc, get_error_body(exc)))
except requests.ConnectionError:
exit_with_error("""
Can't connect to Nailgun server!
@@ -116,3 +116,12 @@ def exceptions_decorator(func):
return {}
return wrapper
def get_error_body(error):
try:
error_body = json.loads(error.response.text)['message']
except (ValueError, TypeError, KeyError):
error_body = error.response.text
return error_body