diff options
author | Jenkins <jenkins@review.openstack.org> | 2014-05-02 09:37:51 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2014-05-02 09:37:51 +0000 |
commit | 86145a666e3dc785ea541f76bf10d4500f916dd3 (patch) | |
tree | a83dc3fd4e040be60826383faaf41efdacb1f9e3 | |
parent | 85b808e20ece0d634347019927b7569afb33e4c2 (diff) | |
parent | 317ce85f2106223f0b06e054a71e0e9b498d0b69 (diff) | |
download | python-ceilometerclient-86145a666e3dc785ea541f76bf10d4500f916dd3.tar.gz |
Merge "Display message on HTTPException"
-rw-r--r-- | ceilometerclient/exc.py | 46 | ||||
-rw-r--r-- | ceilometerclient/tests/test_exc.py | 55 |
2 files changed, 60 insertions, 41 deletions
diff --git a/ceilometerclient/exc.py b/ceilometerclient/exc.py index 57c60c0..f0620b1 100644 --- a/ceilometerclient/exc.py +++ b/ceilometerclient/exc.py @@ -47,6 +47,14 @@ class HTTPException(ClientException): self.details = details def __str__(self): + try: + data = json.loads(self.details) + message = data.get("error_message", {}).get("faultstring") + 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) @@ -65,18 +73,8 @@ class BadRequest(HTTPException): code = 400 -class HTTPBadRequest(BadRequest): - - def __str__(self): - try: - data = json.loads(self.details) - message = data.get("error_message", {}).get("faultstring") - if message: - return "%s (HTTP %s) ERROR %s" % ( - self.__class__.__name__, self.code, message) - except (ValueError, TypeError, AttributeError): - pass - return super(HTTPBadRequest, self).__str__() +class HTTPBadRequest(HTTPException): + code = 400 class Unauthorized(HTTPException): @@ -84,8 +82,8 @@ class Unauthorized(HTTPException): code = 401 -class HTTPUnauthorized(Unauthorized): - pass +class HTTPUnauthorized(HTTPException): + code = 401 class Forbidden(HTTPException): @@ -93,8 +91,8 @@ class Forbidden(HTTPException): code = 403 -class HTTPForbidden(Forbidden): - pass +class HTTPForbidden(HTTPException): + code = 403 class NotFound(HTTPException): @@ -102,8 +100,8 @@ class NotFound(HTTPException): code = 404 -class HTTPNotFound(NotFound): - pass +class HTTPNotFound(HTTPException): + code = 404 class HTTPMethodNotAllowed(HTTPException): @@ -115,8 +113,8 @@ class Conflict(HTTPException): code = 409 -class HTTPConflict(Conflict): - pass +class HTTPConflict(HTTPException): + code = 409 class OverLimit(HTTPException): @@ -124,8 +122,8 @@ class OverLimit(HTTPException): code = 413 -class HTTPOverLimit(OverLimit): - pass +class HTTPOverLimit(HTTPException): + code = 413 class HTTPInternalServerError(HTTPException): @@ -145,8 +143,8 @@ class ServiceUnavailable(HTTPException): code = 503 -class HTTPServiceUnavailable(ServiceUnavailable): - pass +class HTTPServiceUnavailable(HTTPException): + code = 503 #NOTE(bcwaldon): Build a mapping of HTTP codes to corresponding exception diff --git a/ceilometerclient/tests/test_exc.py b/ceilometerclient/tests/test_exc.py index 53ae512..3cdfb85 100644 --- a/ceilometerclient/tests/test_exc.py +++ b/ceilometerclient/tests/test_exc.py @@ -19,33 +19,54 @@ from ceilometerclient import exc from ceilometerclient.tests import utils +HTTPEXCEPTIONS = {'HTTPBadRequest': exc.HTTPBadRequest, + 'HTTPUnauthorized': exc.HTTPUnauthorized, + 'HTTPForbidden': exc.HTTPForbidden, + 'HTTPNotFound': exc.HTTPNotFound, + 'HTTPMethodNotAllowed': exc.HTTPMethodNotAllowed, + 'HTTPConflict': exc.HTTPConflict, + 'HTTPOverLimit': exc.HTTPOverLimit, + 'HTTPInternalServerError': exc.HTTPInternalServerError, + 'HTTPNotImplemented': exc.HTTPNotImplemented, + 'HTTPBadGateway': exc.HTTPBadGateway, + 'HTTPServiceUnavailable': exc.HTTPServiceUnavailable} -class HTTPBadRequestTest(utils.BaseTestCase): +class HTTPExceptionsTest(utils.BaseTestCase): def test_str_no_details(self): - exception = exc.HTTPBadRequest() - self.assertEqual("HTTPBadRequest (HTTP 400)", str(exception)) + for k, v in HTTPEXCEPTIONS.items(): + exception = v() + ret_str = k + " (HTTP " + str(exception.code) + ")" + self.assertEqual(ret_str, str(exception)) def test_str_no_json(self): - exception = exc.HTTPBadRequest(details="foo") - self.assertEqual("HTTPBadRequest (HTTP 400)", str(exception)) + for k, v in HTTPEXCEPTIONS.items(): + exception = v(details="foo") + ret_str = k + " (HTTP " + str(exception.code) + ")" + self.assertEqual(ret_str, str(exception)) def test_str_no_error_message(self): - exception = exc.HTTPBadRequest(details=json.dumps({})) - self.assertEqual("HTTPBadRequest (HTTP 400)", str(exception)) + for k, v in HTTPEXCEPTIONS.items(): + exception = v(details=json.dumps({})) + ret_str = k + " (HTTP " + str(exception.code) + ")" + self.assertEqual(ret_str, str(exception)) def test_str_no_faultstring(self): - exception = exc.HTTPBadRequest( - details=json.dumps({"error_message": {"foo": "bar"}})) - self.assertEqual("HTTPBadRequest (HTTP 400)", str(exception)) + for k, v in HTTPEXCEPTIONS.items(): + exception = v( + details=json.dumps({"error_message": {"foo": "bar"}})) + ret_str = k + " (HTTP " + str(exception.code) + ")" + self.assertEqual(ret_str, str(exception)) def test_str_error_message_unknown_format(self): - exception = exc.HTTPBadRequest( - details=json.dumps({"error_message": "oops"})) - self.assertEqual("HTTPBadRequest (HTTP 400)", str(exception)) + for k, v in HTTPEXCEPTIONS.items(): + exception = v(details=json.dumps({"error_message": "oops"})) + ret_str = k + " (HTTP " + str(exception.code) + ")" + self.assertEqual(ret_str, str(exception)) def test_str_faultstring(self): - exception = exc.HTTPBadRequest( - details=json.dumps({"error_message": {"faultstring": "oops"}})) - self.assertEqual("HTTPBadRequest (HTTP 400) ERROR oops", - str(exception)) + for k, v in HTTPEXCEPTIONS.items(): + exception = v(details=json.dumps( + {"error_message": {"faultstring": "oops"}})) + ret_str = k + " (HTTP " + str(exception.code) + ") ERROR oops" + self.assertEqual(ret_str, str(exception)) |