Merge "Display message on HTTPException"
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user