diff options
author | Mehdi Abaakouk <sileht@redhat.com> | 2016-10-06 16:39:44 +0200 |
---|---|---|
committer | Mehdi Abaakouk <sileht@redhat.com> | 2016-10-07 14:19:23 +0200 |
commit | b8a78396b273c23ba7a633d98365a5dfc969b1f2 (patch) | |
tree | 70c70183d393e11fc8f785ac2d92ee13e5cba80c | |
parent | 6ed98bd6d67e730fc7dc652dcb020b2883e49916 (diff) | |
download | python-ceilometerclient-b8a78396b273c23ba7a633d98365a5dfc969b1f2.tar.gz |
Set code and details on HTTPException
Even the HTTPException is unknown for ceilometer we should set the code
and print it with the details.
Closes-bug: #1626404
Change-Id: Ib244d8822f7a1ebc1b8ec1b95d13b20bbb69ece0
-rw-r--r-- | ceilometerclient/exc.py | 31 | ||||
-rw-r--r-- | ceilometerclient/tests/unit/test_exc.py | 7 |
2 files changed, 25 insertions, 13 deletions
diff --git a/ceilometerclient/exc.py b/ceilometerclient/exc.py index 3db1c0c..8d56038 100644 --- a/ceilometerclient/exc.py +++ b/ceilometerclient/exc.py @@ -43,15 +43,21 @@ class HTTPException(BaseException): self.details = details def __str__(self): - try: - data = json.loads(self.details) - message = data.get("error_message", {}).get("faultstring") + message = "" + if self.details: + message = self.details + try: + data = json.loads(self.details) + message = data.get("error_message", "") + if isinstance(message, dict) and "faultstring" in message: + message = "ERROR %s" % message["faultstring"] + except (ValueError, TypeError, AttributeError): + pass + if message: - return "%s (HTTP %s) ERROR %s" % ( - self.__class__.__name__, self.code, message) - except (ValueError, TypeError, AttributeError): - pass - return "%s (HTTP %s)" % (self.__class__.__name__, self.code) + message = " %s" % message + return "%s (HTTP %s)%s" % (self.__class__.__name__, self.code, + message) class HTTPMultipleChoices(HTTPException): @@ -129,5 +135,10 @@ def from_response(response, details=None): # it is something unexpected raise TypeError("Function 'from_response' expects only response object" " from httplib or requests libraries.") - cls = _code_map.get(code, HTTPException) - return cls(details) + cls = _code_map.get(code) + if cls is None: + exc = HTTPException(details) + exc.code = code + return exc + else: + return cls(details) diff --git a/ceilometerclient/tests/unit/test_exc.py b/ceilometerclient/tests/unit/test_exc.py index b0fd1b1..e67cfc8 100644 --- a/ceilometerclient/tests/unit/test_exc.py +++ b/ceilometerclient/tests/unit/test_exc.py @@ -41,7 +41,7 @@ class HTTPExceptionsTest(utils.BaseTestCase): def test_str_no_json(self): for k, v in HTTPEXCEPTIONS.items(): exception = v(details="foo") - ret_str = k + " (HTTP " + str(exception.code) + ")" + ret_str = k + " (HTTP " + str(exception.code) + ") foo" self.assertEqual(ret_str, str(exception)) def test_str_no_error_message(self): @@ -54,13 +54,14 @@ class HTTPExceptionsTest(utils.BaseTestCase): for k, v in HTTPEXCEPTIONS.items(): exception = v( details=json.dumps({"error_message": {"foo": "bar"}})) - ret_str = k + " (HTTP " + str(exception.code) + ")" + ret_str = (k + " (HTTP " + str(exception.code) + ") " + + str({u'foo': u'bar'})) self.assertEqual(ret_str, str(exception)) def test_str_error_message_unknown_format(self): for k, v in HTTPEXCEPTIONS.items(): exception = v(details=json.dumps({"error_message": "oops"})) - ret_str = k + " (HTTP " + str(exception.code) + ")" + ret_str = k + " (HTTP " + str(exception.code) + ") oops" self.assertEqual(ret_str, str(exception)) def test_str_faultstring(self): |