summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ceilometerclient/exc.py14
-rw-r--r--ceilometerclient/tests/unit/test_exc.py16
2 files changed, 28 insertions, 2 deletions
diff --git a/ceilometerclient/exc.py b/ceilometerclient/exc.py
index c7e550b..3db1c0c 100644
--- a/ceilometerclient/exc.py
+++ b/ceilometerclient/exc.py
@@ -118,6 +118,16 @@ for obj_name in dir(sys.modules[__name__]):
def from_response(response, details=None):
- """Return an instance of an HTTPException based on httplib response."""
- cls = _code_map.get(response.status, HTTPException)
+ """Return an instance of an HTTPException based on http response."""
+ if hasattr(response, "status"):
+ # it is response from HTTPClient (httplib)
+ code = response.status
+ elif hasattr(response, "status_code"):
+ # it is response from SessionClient (requests)
+ code = response.status_code
+ else:
+ # 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)
diff --git a/ceilometerclient/tests/unit/test_exc.py b/ceilometerclient/tests/unit/test_exc.py
index db8df32..b0fd1b1 100644
--- a/ceilometerclient/tests/unit/test_exc.py
+++ b/ceilometerclient/tests/unit/test_exc.py
@@ -69,3 +69,19 @@ class HTTPExceptionsTest(utils.BaseTestCase):
{"error_message": {"faultstring": "oops"}}))
ret_str = k + " (HTTP " + str(exception.code) + ") ERROR oops"
self.assertEqual(ret_str, str(exception))
+
+ def test_from_response(self):
+ class HTTPLibLikeResponse(object):
+ status = 400
+
+ class RequestsLikeResponse(object):
+ status_code = 401
+
+ class UnexpectedResponse(object):
+ code = 200
+
+ self.assertEqual(HTTPLibLikeResponse.status,
+ exc.from_response(HTTPLibLikeResponse).code)
+ self.assertEqual(RequestsLikeResponse.status_code,
+ exc.from_response(RequestsLikeResponse).code)
+ self.assertRaises(TypeError, exc.from_response, UnexpectedResponse)