From 533b453d208c08d013db2592cb11c246ce8613e5 Mon Sep 17 00:00:00 2001 From: Maciej Kwiek Date: Mon, 2 Mar 2015 12:13:57 +0100 Subject: [PATCH] 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 --- fuelclient/cli/error.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/fuelclient/cli/error.py b/fuelclient/cli/error.py index 73f5688..5fbe67d 100644 --- a/fuelclient/cli/error.py +++ b/fuelclient/cli/error.py @@ -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