From ebcaa2aabbed98a2a1a322849e8e7a9f73e6b4dd Mon Sep 17 00:00:00 2001 From: tengqm Date: Sat, 27 Feb 2016 01:10:43 -0500 Subject: [PATCH] Fix SDK exception parsing There are cases we are not parsing the SDK exceptions properly. This patch is an attempt to fix this problem. Change-Id: I9ba7756587e87b74b66d8b871e16bcf4700603e9 --- senlinclient/common/exc.py | 39 +++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/senlinclient/common/exc.py b/senlinclient/common/exc.py index 30866821..e5e0a6a5 100644 --- a/senlinclient/common/exc.py +++ b/senlinclient/common/exc.py @@ -233,16 +233,37 @@ def parse_exception(exc): :param details: details of the exception. """ if isinstance(exc, sdkexc.HttpException): - try: - record = jsonutils.loads(exc.details) - except Exception: - # If the exc.details is not in JSON format - record = { - 'error': { - 'code': exc.http_status, - 'message': exc, + if exc.details is None: + data = exc.response.json() + code = data.get('code', None) + message = data.get('message', None) + error = data.get('error', None) + if error: + record = { + 'error': { + 'code': exc.http_status, + 'message': message or exc.message + } + } + else: + info = data.values()[0] + record = { + 'error': { + 'code': info.get('code', code), + 'message': info.get('message', message) + } + } + else: + try: + record = jsonutils.loads(exc.details) + except Exception: + # If the exc.details is not in JSON format + record = { + 'error': { + 'code': exc.http_status, + 'message': exc, + } } - } elif isinstance(exc, reqexc.RequestException): # Exceptions that are not captured by SDK record = {